1

I need to send an email when an admin changes a users role to role x. The email should not be sent if the users role is already role x and 'save' is pressed on the user page.

I have set up a rule:

Event: After updating an existing user account

Actions: Send mail

This works fine. Then I add a condition of type 'data comparison' account(updated user):roles with the operator 'equals' and the data value 'role x'. But for some reason the email now never sends when I change the user role to 'role x' and resave. I'm confused about why this is not working.

Then once this is fixed I assume I'll need to negate the condition that the account(unchanged user):roles equals 'role x'

2

Key according to me will be

Using condition as User has Roles choose "x" as user role and negate it.

An example export goes here.

{ "rules_update_user_role" : {
    "LABEL" : "Update User Role",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "user_update" : [] },
    "IF" : [
      { "user_has_role" : {
          "account" : [ "account-unchanged" ],
          "roles" : { "value" : { "4" : "4" } }
        }
      }
    ],
    "DO" : [ { "drupal_message" : { "message" : "Changed User!" } } ]
  }
}
4

Arpitr got me on to the correct solution for this. Here is the code that you can import:

{ "rules_update_user_role" : {
"LABEL" : "Update User Role",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"ON" : { "user_update" : [] },
"IF" : [
  { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "7" : "7" } } } },
  { "AND" : [
      { "NOT user_has_role" : {
          "account" : [ "account-unchanged" ],
          "roles" : { "value" : { "7" : "7" } }
        }
      }
    ]
  }
],
"DO" : [ { "drupal_message" : { "message" : "Changed User!" } } ]

} }

I like the way arpitr used a drupal message for testing if the rule had worked - i didn't realise you could do this.

It looks like the mistake I might have been making was to use the condition of type 'data comparison' and then try to drill into it to get to the user role rather than just using the 'user has role' condition. I think when i had tried previously to use the 'user has role' condition it failed because I made the used 'account:roles' rather than just account.

Hope this helps someone else.

  • nice explanation :) – arpitr Mar 19 '14 at 14:10
  • Awesome solution, thanks! My users with "Role X" were receiving repeated notifications because I didn't have the "account-unchanged" condition. – hockey2112 Sep 15 '16 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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