So you want to automatically fill in the entity reference field on a newly created node with a reference to a node which is dynamically created 'after saving content', huh?

Modules used: entity reference and rules in Drupal 7. (basic sandbox setup)
So beneath the steps in your rule.

1. After saving new content of type 'story'
2. Create a new entity
Parameter: Entity type: Node, Content type: article, Title: article related to nid..., Author: 2
Provides variables: Created article (article_created)
3. Save entity
Parameter: Entity: [article-created], Force saving immediately: true
4. Set a data value
Parameter: Data: [node:field-myref], Value: [article-created]

Before you execute this rule, though, you should create an article node (I called mine 'a generic article') that you are going to use as the initial value set in the entity reference field of the node you're about to create.
Once you've created that generic article, go to the manage fields tab of the story content type and select 'a generic article' as the default value. That way, the entity reference field is set initially after the 'save entity' action and the 'set data value' action can take place.


jagadmarx’s picture

Thanks for this, it helped me a lot. :)

mooru’s picture

I have a situation where i have an entity reference field that uses the entity reference views widget to store multiple values. When i do a loop of the content type that has the reference field i can loop through the list of nodes created. When i do a show message on site with rules i have no problem but when i try to do a set data value in the other content types node that holds the referenced value, i can't.

Is there any way around this?

thememex’s picture

mooru - At step 4 above, use instead the "Add an item to a list" option.

Also review the option to enforce uniqueness. This can be helpful when you have a single parent record with multiple child record references.

Add an item to a list
Parameter: List: [entity:field-multi-ref], Item to add: [create-single-record], Enforce uniqueness: true

pinueve’s picture

Thanks for posting this. Default value settings didn't work for me, what I did (between step 3 and 4) is "fetch entity by id", grab nid, and next action is step 4, and that worked.
Bassplaya answer works.