I apologize if this is a no-brainer question...none of my searches have found answers that are on-point. I am trying to use fields from a fetched entity as a token in a direct input field of a rule action.
The rule I'm constructing is an email rule that emails license keys to customers who have made online purchases. I have a license key table that is related to orders, and I have defined that table as an entity through the entity API.
When the purchase event triggers, I have specified an "entity exists by property condition." The condition makes sure that there is an instance of my custom entity that's related to the order id in question.
Two actions result when that condition is met. First there's a "fetch entity by property." I fetch my custom entity by the related orderid value. Second, there's a "send mail" action. In the body of that email, I would like to include a token representing a field value of the fetched entity. I can do this if I set the email body to "data selector." The selector allows gives me the option [entity-fetched:0:desired_field]. This produces an email with a body that contains the data from the desired field.
Now, here's the issue. In an email body, you never just want one piece of data. You want a direct input scenario, where you can enter message text and tokens for variable fields. When I switch from data selector to direct input, and try to use [entity-fetched:0:desired_field] as a token, the resulting email does not interpret it as a token. It merely shows me the literal text "[entity-fetched:0:desired_field]".
It feels like I am so frustratingly close, and this must be a common problem. But I cannot find any answers on the issue. What am I doing wrong?
Comments
Comment #1
fenster CreditAttribution: fenster commentedComment #2
Dave ReidI'm not sure I'll be able to help you better than the Rules maintainers, so I'll move this issue back into their queue.
Comment #3
mitchell CreditAttribution: mitchell commentedComment #4
nmc CreditAttribution: nmc commentedI believe #1336826: Chained object token replacements not working is related or a duplicate of this.
Comment #5
mitchell CreditAttribution: mitchell commentedUpdated component.
Comment #6
jlporter CreditAttribution: jlporter commentedhttp://drupal.org/project/rules_conditional
Get this module and add an if condtional that checks if the field exists on the entity you fetched. Then it is magically available in the data selector. I had to delete my create entity action(php error when I just dragged into condtional). After creating a fresh create entity action within the if entity has field I was able to select that field.
It does however baffle me that fetch entity in rules doesn't fetch the whole entity(or at least doesn't expose it)
Comment #7
eminencehealthcare CreditAttribution: eminencehealthcare commentedThanks jlporter, this helped me so much. I am also baffled on why fetch entity does not fetch all the fields associated with that entity automatically.
Comment #8
BassPlaya CreditAttribution: BassPlaya commentedOh yeah! So cool! Cheers!
Just to give some extra info, you'll have to add the action 'inside' that conditional otherwise you won't see the field of that fetched entity.
Comment #9
miromarchi CreditAttribution: miromarchi commented#6 is working, thank you very much, I spent days on this. thanks
Comment #10
jetwodru CreditAttribution: jetwodru commentedAgree, I'm totally crippled without Conditional Rules coz the concept of Rules goes far beyond my existing knowledges of programming logic in workflow. Conditional Rules empowers Rules to the greatest level of usability.
Comment #11
Mehrdad201 CreditAttribution: Mehrdad201 commentedThanks a lot Dear jlporter
Your mentioned module really helped me.
Comment #12
jweirather CreditAttribution: jweirather commented+1 for #6: http://drupal.org/project/rules_conditional
Specifically working with fetched and created entities, which were not available in the standard "conditions" section. This worked exactly as I needed, adding "entity has fields" conditions in the "actions" section after fetching/creating the entities, which granted access to the values/fields that were otherwise unavailable.
Comment #13
jeroenwa CreditAttribution: jeroenwa commented+1 for #6: http://drupal.org/project/rules_conditional