Why does my Rules Condition about registration code seem to fail? I'm using the Rules module and the Registration code module and I've tried configuring a rule like so:


Rules Event: upon saving a new user account(event).
Rules Condition: if the registration code used is "123456"(condition).
Rules Action: add a role for a user.


The rules export is as shown below:


{ "rules_give_trial_version" : {
    "LABEL" : "Give trial version",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "regcode" ],
    "ON" : { "regcode_used" : [] },
    "IF" : [ { "data_is" : { "data" : [ "regcode" ], "value" : "123456" } } ],
    "DO" : [
      { "user_add_role" : { "account" : [ "user" ], "roles" : { "value" : { "10" : "10" } } } }
    ]
  }
}


However, the Rules Action is not working (nothing works).

Any idea how to get this to work?

Note: The Rules Action works with the event "User has used a registration code" without any Rules Condition (if I temporarily remove that Rules Condition). But I'm badly in need of the condition as we want to check if a user has used a specific code. If yes, that user should be assigned a role.
The typical Rules Condition doesn't seem to work here

Typically when using Rules, you can always give it a try to see if you can add, as a FIRST Rules Condition, a condition like "Entity has field". Or any other variation of a condition that informs the Rules module that you want to test the value of that field in a subsequent rules condition. Without that, Rules has no clue about which data to make available for subsequent processing ...

But, that doesn't seem to work/help for this particular case. For more details on that (if you really want to know why it doesn't work), you may want to look at the active Major task (not bug!) about Make better use of the Entity system ... (from Fago, who has quite a Rules "reputation" ...). 

Rules debugging

If that doesn't help, I typically add a (temporary) Rules Action to just "Show a message on the site" (eg to show the value of a field ...). That might result in other unexpected results, to help you further debug it ...

But that doesn't seem to help either (grrrr), so time to move on to Rules debugging, as explained in my answer to "How can I display the value of a variable inside a condition?".

Depending on what the result of that type of debugging is, you may also want to (temporary) enable the Devel module, so that you can (temporary) add a Rules Action like "Display a value".

Try to find (and read) the documentation

Too bad, this module doesn't seem to have a lot of documentation, and the README.txt is not of much help either.

If anything else fails, read the sources

OK, the advantage of open source ... lets go read the sources ...

Aha, I see the regcode.rules.inc (regcode in this filename is the machine name of the Registration code module), so let's see what this module's Rules integration has to offer:


A Rules Event with machine name "regcode_used" and label "User has used a registration code". Fine, that's also what's used in the rule in the question here (nothing new).
A Rules Condition with (a loooong) machine name "regcode_rules_condition_regcode_available" and label "User has used a registration code". Huh??? That's the same label as in the Rules Event, confusing!!! But that Rules Condition is what intrigues me. Time to fabricate a variation of the rule in the question.


Rule prototype (1st attempt)

Have a look at this variation of the Rule in the question (also in Rules export format, use the Rules UI to import it in your own site):

{ "rules_give_trial_version" : {
    "LABEL" : "Give trial version",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "regcode", "rules" ],
    "ON" : { "regcode_used" : [] },
    "IF" : [
      { "regcode_rules_condition_regcode_available" : { "user" : [ "user" ] } },
      { "data_is" : { "data" : [ "regcode" ], "value" : "123456" } }
    ],
    "DO" : [
      { "user_add_role" : { "account" : [ "user" ], "roles" : { "value" : { "10" : "10" } } } }
    ]
  }
}


The only difference in this rule (as compared to the original rule in the question) is the very first (confusing labeled) Rules Condition with machine name regcode_rules_condition_regcode_available.

That additional Rules Condition seems pretty close to the typical Rules Condition as I mentioned in the beginning of my answer (and Fago's issue I mentioned before makes a lot of sense to me now). And since the question states "... badly in need of the condition ...", I thought I'd leave the QA-testing of this variation of the rule in the question to the OPer of the question ...

Too bad, but that 2nd Rules Condition can never work, since the "[regcode]" token is not the token we're looking for (ie something that is a string, and which we want to check if it yes or no matches the registration code we're interested in to assign that special role). Read on for a new (2nd) attempt ...

Rule prototype (2nd attempt)

With some additional eyeballing and experiments, I noticed that within "Rules Actions" (not Rules Conditions!) there is the token [regcode:code], apart from all sorts of other regcode related tokens. So that opens another opportunity to get it to work anyway: let's just grab that [regcode:code] string and pass it on to a (new) Rules Component, in which we're going to do our thing (ie verify the registration code and grant the role if the code is what we want it to be. Below is the adapted rule from the previous attempt, and the (new) Rules Component:

a) Adapted Rule:

{ "rules_grant_a_role_after_using_a_registration_code" : {
    "LABEL" : "Grant a role after using a registration code",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "regcode", "rules" ],
    "ON" : { "regcode_used" : [] },
    "IF" : [
      { "regcode_rules_condition_regcode_available" : { "user" : [ "user" ] } }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "Value of token \u0022regcode:code\u0022 is: [regcode:code] and will be used as parm for the subsequent Rules Action, which invokes a Rule Component." } },
      { "component_rules_grant_role_for_specific_regcode" : { "actual_regcode" : "[regcode:code]" } }
    ]
  }
}


b) Rules Component:

{ "rules_grant_role_for_specific_regcode" : {
    "LABEL" : "Grant role for specific regcode",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "actual_regcode" : { "label" : "Actual Regcode", "type" : "text" } },
    "IF" : [ { "data_is" : { "data" : [ "actual-regcode" ], "value" : "123456" } } ],
    "DO" : [
      { "user_add_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "10" : "10" } }
        }
      },
      { "drupal_message" : { "message" : "Since you used registration code \u0022[actual-regcode:value]\u0022, you have been granted a special role that corresponds to it." } }
    ]
  }
}


Note the 2 extra "Show a message" actions I added (to simplify QA testing).

Curious to hear if with this 2nd attempt it does work, or if you can MAKE it work from here on ...