2

I have two content types (Inventory and Sales). In Sales, I referenced Inventory using the Entity reference module.

In Inventory, I have a stock_available (integer) field. In Sales, I also have the same field, so that I can automatically populate its value from Inventory using the Entity reference autofill module.

When I add a new Sales node, I want to adjust the value of stock_available in the referenced Inventory, so that gradually it becomes zero in the referenced Inventory node.

I know how to set value/calculated value from other filed within same content type, by using the Rules module.

I have no idea how I can do same things to targeted entity reference node. I think it's possible by just rule configuration. Please let me know if something needs to change to get same output that I expected.

0

Step 1: Create a Rules Component

Create a Rules Component (of type "action set") like so:

  • Parameters are "node" and "stock decrease".
  • Rules Action = "set value/calculated value from other filed within same content type" (*).

Here is a prototype of such Rules Component:

{ "rules_decrease_stock" : {
    "LABEL" : "Decrease Stock",
    "PLUGIN" : "action set",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "node_to_update" : { "label" : "Node to be updated", "type" : "node" },
      "stock_decrease" : { "label" : "Stock decrease", "type" : "integer" }
    },
    "ACTION SET" : [
      { "drupal_message" : { "message" : "Something just got sold, so please decrease the stock for node with id [node-to-update:nid] with an amount of \r\n[stock-decrease:value] units. To actually do so, replace this (informational) Rules Action with setting the appropriate value for the appropriate field ..." } }
    ]
  }
}

The actual action only displays a Drupal message (to show the values of the variables passed to it). Replace that action with the actual update of your inventory node (*).

Step 2: Create a Rule

Create a rule with Rules Event something like "after updating a node of content type "Sales". Maybe also some Rules Condition like "stock_available" is updated (checked if the unsaved value differs from the saved value). And add these Rules Actions:

  • Update the stock_available in your Sales content type (*).
  • Perform the Rules Component (from step 1), whereas you pass these 2 parameters:

    • the node, which you can pass via the value (token) of the entity reference.
    • the stock decrease amount (which is identical).

(*): you indicated in your question you know how to do that ...

  • Sir, I am confused, if it's possible please explain it little more (screenshot, if possible). If I create rules components then what "Component plugin" should I choose? and in DATA TYPE what I choose? In action within Components, I didn't find filed "stock_available" in Data selector. how to pass parameters? Actually I need to update/change value to filed in Inventory content type, when I add sales content (Stock Available - Sales Quantity). I hope you understand me now. – Md. Jakaria Jul 22 '16 at 10:33
  • Sorry for late response. Yeah, I solve this problem but without using component option. I was found a tutorial on this, just with little change, I was able to get expected output. Should I write those step here? – Md. Jakaria Jul 23 '16 at 16:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.