1

I have a webform, that the HR use. I have a form component of the users, that is a user reference field.

I would like to use Webform Rules to send an e-mail notification to the selected user.

If I use: [data:{component}-display], then after the username [1] appears, that refers to the link to the user.

0

Even though your question doesn't say so (yet), I'm assuming (as per your "... I have a form component of the users, that is a user reference field...") that you're also using the Webform References module. Some details about that module (from its project page):

Provides a webform component that can reference a node/term/user in Webform. It provides "Select List", "Autocomplete", "Checkbox" and "Radio" as widget.

If you also have the Webform Rules module enabled, then it is possible to create a rule (using Rules), which does what you're asking for. Here is an export of a rule that will send an eMail to the eMail Id of the referenced user:

{ "rules_email_submitted_webform_to_referenced_user" : {
    "LABEL" : "eMail submitted webform to referenced user",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "webform_rules", "rules" ],
    "ON" : { "webform_rules_submit" : [] },
    "IF" : [
      { "webform_has_id" : {
          "form_id" : [ "form_id" ],
          "selected_webform" : { "value" : { "webform-client-form-191" : "webform-client-form-191" } }
        }
      }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "The webform submitted contains the field labeled \u0022[data:demoform_userref-title]\u0022 which has value \u0022[data:demoform_userref-value-raw]\u0022." } },
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "[data:demoform_userref-value-raw]" },
          "PROVIDE" : { "variable_added" : { "referenced_user_txt" : "Referenced user" } }
        }
      },
      { "data_convert" : {
          "USING" : { "type" : "integer", "value" : [ "referenced-user-txt" ] },
          "PROVIDE" : { "conversion_result" : { "referenced_user_uid" : "Referenced User Uid" } }
        }
      },
      { "entity_fetch" : {
          "USING" : { "type" : "user", "id" : [ "referenced-user-uid" ] },
          "PROVIDE" : { "entity_fetched" : { "user_fetched" : "Fetched user" } }
        }
      },
      { "drupal_message" : { "message" : "referenced_user_txt: [referenced-user-txt:value] AND referenced_user_uid: [referenced-user-uid:value] AND\r\n\r\nUser fetched: uid=[user-fetched:uid] name=[user-fetched:name] mail=[user-fetched:mail]" } },
      { "mail" : {
          "to" : [ "user-fetched:mail" ],
          "subject" : "Hey [user-fetched:name] you have new mail ...",
          "message" : "The webform submitted contains these fields:\r\n\u003C\/ul\u003E\r\n\u003Cli\u003Ethe field labeled \u0022[data:demoform_title-title]\u0022 which has value \u0022[data:demoform_title-value-raw]\u0022.\u003C\/li\u003E\r\n\u003Cli\u003E the field labeled \u0022[data:demoform_text-title]\u0022 which has value \u0022[data:demoform_text-value-raw]\u0022.\u003C\/li\u003E\r\n\u003Cli\u003E the field labeled \u0022[data:demoform_number-title]\u0022 which has value \u0022[data:demoform_number-value-raw]\u0022.\u003C\/li\u003E\r\n\u003Cli\u003E the field labeled \u0022[data:demoform_option-title]\u0022 which has value \u0022[data:demoform_option-value-raw]\u0022.\u003C\/li\u003E\r\n\u003C\/ul\u003E",
          "language" : [ "" ]
        }
      }
    ]
  }
}

Some more details about this rule (prior to trying to import this rule in your own site, make sure to adapt (rename) what's needed to fit your case):

  • The form ID I'm using is 191.
  • All form components have machine names starting with demoform.
  • The machine name of the reference user is demoform_userref.

The Rules Event and Rules Condition should be straight forward, though the actual Rules magic happens in the Rules Actions:

  1. Show a message on the site (just for demo purposes).
  2. Add a text variable (to capture "some" webform data about the selected user, has to be text ...).
  3. Convert a variable from a text to an integer variable (we need an integer for the next Rules Action, text won't work ...).
  4. Fetch an entity by ID (the integer variable, to fetch the user with that ID).
  5. Show a message on the site (just for demo purposes).
  6. Send an eMail (to the eMail ID from the fetched user).

Demo

After submitting my demoform, the messages shown are like so:

The webform submitted contains the field labeled "User" which has value "123".
referenced_user_txt: 123 AND referenced_user_uid: 123 AND User fetched: uid=123 name=Pierre.Vriens mail=pierre.vriens@somewhere.com

Moreover, an eMail got delivered to pierre.vriens@somewhere.com ...

Subject:

  Hey Pierre.Vriens you have new mail ...

Message:

  The webform submitted contains these fields:
   * the field labeled "Title" which has value "Some title".
   * the field labeled "Text" which has value "Some text".
   * the field labeled "Number" which has value "7".
   * the field labeled "Option" which has value "C". 
| improve this answer | |

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.