How to set field value after create a content type.
I have a content type TEST and set this actions:

  1. Reacts on event: Entity insert
  2. Add Action: Set field value->Target Entity Type: Content->Target Bundle: TEST
  3. Select Field: Field_test-> Set Value: "Test"

Then I create content of type TEST, but the field is not fill with value set in the rules.

Where I'm wrong?

Thanks.

Comments

Kris77 created an issue. See original summary.

c.e.a’s picture

It seems the action "Set field value" action is not working for the latest beta version...

Do the below for a good alternative workaround...

Reacts on the event:
Type: After entity update
Target Entity Type: Content
Target Bundle: Article

Create a variable:
Type: Empty Entity variable
Target Entity Type: Content
Target Bundle: Article

Action#1:
Type: Fetch entity variable by id
Target Entity Type: Content
Target Bundle: Article
Entity id field: ID [Nid]
Variable: Choose the created Empty Entity variable.
Value: [node:nid]

Action#2:
Type: Set values to entity variable.
Variable: Choose the created Empty Entity variable.

Action#3:
Type: Save entity variable.
Variable: Choose the created Empty Entity variable.

Check my video:
https://www.youtube.com/watch?v=YTzHezupA9Y

c.e.a’s picture

Status: Active » Closed (works as designed)
publishing future’s picture

Status: Closed (works as designed) » Needs work

Thank you for the workaround. But why is this issue closed and set to "works as designed"?
If the action "Set field value" action is not working, this should nevertheless be fixed. I still encounter the same problem with the latest dev. Therefore set back to "Needs work".

c.e.a’s picture

I found out the "Set field value" action is working only in very specific cases but generally it is not...

This issue is added now to the important bug report list of issues #3041221: List of all 'Bug reports' that need immediate support Assigned to: yseki

Thank you for pointing out on this issue.

c.e.a’s picture

Category: Support request » Bug report
Priority: Normal » Major
lexsoft00’s picture

Hi,

If you are using tokens it might be that it will only work if few conditions are met and the token value is available. It will also not work if you are trying to modify an entity that does not exist yet. Example on entity insert will not work since the entity has not been saved, it will work if you are modifying another bundle then the same one that rule event uses.

If you would like the action to work with different bundles you should try the patch posted here https://www.drupal.org/project/business_rules/issues/3033723 even if it's not ideal and only a sandbox patch.

I will try to make a proper patch to add this functionality as an extension to the existing action type.

Hope this helps

c.e.a’s picture

@lexsoft the 'set field value' action is not working even for the simplest cases.

Example:

RULE
Entity is viewed

  • Target Entity Type: User.
  • Target Bundle: User.

ACTION
Set field value.

  • Target Entity Type: User.
  • Target Bundle: User.
  • Field: field_user_family_name
  • Value: My new family name

Logged in as uid 1, after I view my profile, then I go to /user/1/edit page, I expect to see the field_user_family_name value = My new family name but the value of the field is still empty or equal to previous value.

Thank you,

colan’s picture

Status: Needs work » Active

There's no code here so updating status. As usual, patches welcome.

c.e.a’s picture

Version: 8.x-1.0-beta6 » 8.x-1.0-beta8
Issue tags: +br-beta-9
yseki’s picture

Version: 8.x-1.0-beta8 » 8.x-1.x-dev
Status: Active » Patch (to be ported)
StatusFileSize
new834 bytes

It's is a misuse of the action.
This action is applicable only during the saving event, that corresponds to entity_presave hook or "Before saving entity" rule.

Here is a patch that filters the action to not be available for the wrong rules.

  • yuriseki committed 8322a36 on 8.x-1.x
    Issue #3019450 by yuriseki, C.E.A, Kris77, Publishing Future, lexsoft,...
colan’s picture

Status: Patch (to be ported) » Fixed

Fixing status.

djween’s picture

1. Using event: Before Saving Entity:
--I can add action: Set field value which is good. However,
--on same rule if I add a condition and need to set a field based on a condition, I cannot add action: Set field value within the condition on that same rule. To clarify - I can add Set field value if condition is to compare data values and set to bundle but I can't add Set field value if comparing variables. Is this by design?

2. Additionally: workaround in comment #2, for me I found that tokens are pulling in values for most recently created node no matter which node I subsequently update. As a result, node is being updated with incorrect data. Scratch this second comment. I have a very specific use case where I am not able to do this and have a separate issue opened for it: https://www.drupal.org/project/business_rules/issues/3136784

Status: Fixed » Closed (fixed)

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