I already know how to do it in D6 Rules 1.x but can't seem to figure out how to accomplish this in D7 Rules 2.x, I basically just want to increment a value of a field by 1 on a node save. can anyone please point me in the right direction. I have everything set up on the rule just can't increment by 1, I've tried using execute php but I might not be using it right.

Update: I figured it out

Comments

Desi Raaj’s picture

Component: Rules Core » Rules Engine
Status: Active » Fixed
rwilson0429’s picture

Glad you figured it out. I (and probably lots of others) have been trying to figure out the same thing. Would you share your solution? Thx.

webroru’s picture

+1

Desi Raaj’s picture

There are multiple way of doing this, but here is mine:

My Scenario: I wanted to increment a value of a user field that I created by 1 every time the user saved a node.

Rule:
Event: After saving new content

Conditions: Content is of type
- Data Selector: Node
- Value: [pick your content type(s)]

Action:
Fetch entity by id
-Value: User
-Data Selector: node:author:uid
-Provided variables: [leave as is or change if you want]
Execute custom PHP code
-$entity_fetched->"your_field"[LANGUAGE_NONE][0]['value'] = $entity_fetched->"your_field"[LANGUAGE_NONE][0]['value'] + 1;
Save Entity
-Data Selector: node:author

rwilson0429’s picture

Thanks. I want to automatically assign a client ID to a node when it is created. I want the id's to increment by 1 based on the highest value of the ids that have already been saved. Thought about just using the nid but, it is not specific to the content type. I would have thought that this would be simple to do without php coding but, I've run into a stumbling block. I will try your method to see if it works for me. Thanks again for posting your solution.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

IWasBornToWin’s picture

at #5 you should be able to do that in the backend at mysql. If you can get to the field in the structure of the table there is a setting you can check to increment the value of a field by 1.

rwilson0429’s picture

@IWasBornToWin: Interesting. I am familiar with mysql ability to auto-increment a field's value but, I didn't know it would work properly in Drupal's table schema. I will give it a try. Thanks.

For now, I have accomplished the assignment of a value to the client number field using the Computed fields module and custom php code:

if (empty($entity->field_client_number[$entity->language][0]['value'])) 
	{
    $bundle = $entity->type;
    $result = db_query("SELECT MAX(id.field_client_number_value) FROM {field_data_field_client_number} id WHERE id.bundle = :bundle", array(':bundle' => $bundle))->fetchField();
    $entity_field[0]['value'] = $result + 1;
	}
spineless’s picture

Component: Rules Engine » Rules Core

I am getting a weird error when I attempt to create a rule using php. What I want to do is increment a field when a comment is added. Here is my rule.

{ "rules_increment_comment_count" : {
"LABEL" : "increment_comment_count",
"PLUGIN" : "reaction rule",
"TAGS" : [ "Project_Management" ],
"REQUIRES" : [ "rules", "php", "comment" ],
"ON" : [ "comment_insert" ],
"IF" : [
{ "node_is_of_type" : {
"node" : [ "comment:node" ],
"type" : { "value" : { "mvp_customer_question" : "mvp_customer_question" } }
}
}
],
"DO" : [
{ "entity_fetch" : {
"USING" : { "type" : "node", "id" : [ "comment:node:field-total-comment-count" ] },
"PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
}
},
{ "php_eval" : { "code" : "$entity_fetched-\u003E\u0022field_total_comment_count\u0022[LANGUAGE_NONE][0][\u0027value\u0027] = $entity_fetched-\u003E\u0022field_total_comment_count\u0022[LANGUAGE_NONE][0][\u0027value\u0027] + 1;" } },
{ "entity_save" : { "data" : [ "entity-fetched" ] } }
]
}
}

When I execute this rule I get the following Rules error.

0 ms Rule incrament_comment_count fires.
1.541 ms Evaluating the action entity_fetch. [edit]
4.201 ms Unable to load node with id "4"
5.31 ms Unable to evaluate action entity_fetch. [edit]
5.585 ms Evaluating the action php_eval. [edit]
6.758 ms Unable to get variable entity_fetched, it is not defined.
7.884 ms Unable to evaluate action php_eval. [edit]
9.043 ms Unable to get variable entity_fetched, it is not defined.
10.114 ms Unable to evaluate action entity_save. [edit]
10.214 ms Rule increment_comment_count has fired.

The number 4 is the value in the field. The idea is that every time someone adds a comment the comment count increments. I need this because I want to display the approved and unapproved comments.

Any help would be great.

spineless’s picture

I was able to get this to work with the help of spovlot.

All you need to do is in actions

calculate a value
set a data value.

spineless’s picture

Issue summary: View changes

Solved

nanabrownee’s picture

Issue summary: View changes

Has anyone managed to sort it out with rules conditional?

Can't get it to work!
Or an alternative?

this is my export:
{ "rules_set_field_int_breakout_when_session_flagged" : {
"LABEL" : "Set field int breakout when session flagged",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "flag", "rules_conditional", "rules" ],
"ON" : {
"flag_flagged_int_breakout_1" : [],
"flag_flagged_external_breakout_session" : []
},
"DO" : [
{ "flag_fetch_node_by_user" : {
"USING" : { "flag" : "int_breakout_1", "flagging_user" : [ "flagging_user" ] },
"PROVIDE" : { "content_flagged_by_user" : { "content_flagged_by_user" : "Content flagged by user" } }
}
},
{ "CONDITIONAL" : [
{
"IF" : { "node_is_of_type" : {
"node" : [ "flagged-node" ],
"type" : { "value" : { "session" : "session" } }
}
},
"DO" : [
{ "CONDITIONAL" : [
{
"IF" : { "NOT data_is" : {
"data" : [ "flagging-user:profile-main:field-internal-breakout-selected" ],
"value" : "Yes"
}
},
"DO" : [
{
"WHILE" : { "data_is" : {
"data" : [ "flagged-node:field-assigned-seats" ],
"op" : "\u003C",
"value" : [ "flagged-node:field-max-seats" ]
}
},
"DO" : [
{ "data_set" : {
"data" : [ "flagging-user:profile-main:field-internal-breakout-selected" ],
"value" : "Yes"
}
},
{ "data_set" : {
"data" : [ "flagging-user:profile-main:field-internal-session-breakout" ],
"value" : [ "flagged-node" ]
}
},
{ "data_calc" : {
"USING" : {
"input_1" : [ "flagged-node:field-assigned-seats" ],
"op" : "+",
"input_2" : "1"
},
"PROVIDE" : { "result" : { "result" : "Calculation result" } }
}
},
{ "data_set" : {
"data" : [ "flagged-node:field-assigned-seats" ],
"value" : [ "result" ]
}
}
]
}
]
}
]
}
]
},
{
"ELSE IF" : { "node_is_of_type" : {
"node" : [ "flagged-node" ],
"type" : { "value" : { "external_session" : "external_session" } }
}
},
"DO" : [
{ "CONDITIONAL" : [
{
"IF" : { "NOT data_is" : {
"data" : [ "flagging-user:profile-main:field-external-session-selected" ],
"value" : "Yes"
}
},
"DO" : [
{ "data_set" : {
"data" : [ "flagging-user:profile-main:field-internal-session-breakout" ],
"value" : [ "flagged-node" ]
}
},
{ "data_set" : {
"data" : [ "flagging-user:profile-main:field-internal-breakout-selected" ],
"value" : "Yes"
}
}
]
}
]
}
]
}
]
}
]
}
}

najamfzl’s picture

One easy way is to use "offset" in "Set a data value" action. I came across this problem when I needed to count the items a site user has added to his cart or temporary storage. I added a variable (integer type) before initiating a loop on the nodes and then add node to a list (Add item to a list action) and after that used "Set data value" action with both field same (my variable) but just added "1" in offset. If someone is doing the same thing, it is very important to initialize the variable with zero (0) value and do not leave it blank. I hope it helps someone as it did to me.