Question

Photo of Derek Mangrum

2

Email Group Members from a Workflow

I am working on creating some additional Facilities Requests for our Facilities teams to handle work orders. I have copied the provided Facilities Request Workflow and am making the needed changes. I have run into a need to have my Workflow send an email to the members of a group. The 'Send Email' action type lets you manually enter SMTP address(es) or reference an attribute value. It doesn't appear that you can target a Group and have emails sent to each group member.

If there is an easy way, please let me (us, the community) know. Or, this could be a call/request for a 'Send Email to Group' Workflow Action.

I have, however, created a workaround that I wanted to share.

First, there are three Workflow Attributes that you will need. 1) an attribute to hold the Group you want to target, 2) an attribute to hold the raw email address list, 3) an attribute to hold the trimmed email address list. I found that, when manually entering email addresses in the 'Send Email' action, a comma-delimited list works for sending email to multiple recipients. This delimited list must be well-formed, though... no trailing commas, for example.

group-email-01.PNG

I set the 'Worker Group' attribute default value to be the group that I will be sending email to in my workflow.

I then have three actions that create and send the email to the group members. The first action is a 'Set Attribute Value' configured like this:

group-email-02.PNG

As you can see, the magic happens thanks to the power of Lava! The full text of the "Text Value" field is:

{% assign grp = Workflow | Attribute:'WorkerGroup','Object' %}{% for member in grp.Members %}{% if member.Person.IsEmailActive == true and member.GroupMemberStatus == 1 and member.Person.RecordStatusValue.Name != empty and member.Person.RecordStatusValue.Name != 'Inactive' and member.Person.Email != '' %}{{ member.Person.Email | Downcase }},{% endif %}{% endfor %}

Briefly, this Lava assigns the WorkerGroup, as an object, to the 'grp' variable. Then, I loop through the .Members collection of that grp variable. For each 'member', I do various checks and then add the SMTP address followed by a comma. Once complete, this creates a string similar to 'email1@abc.com,email2@xyz.com,' and stores it in the Group Member Emails attribute.

That trailing comma is a problem, so I have to get rid of it. The next action does that for me.

group-email-03.PNG

Once again, Lava works well here. The full text is:

{% assign valLength = Workflow.GroupMemberEmails | Size | Minus:1 %}{{ Workflow.GroupMemberEmails | Truncate:valLength,'' }}

This just gets the length of the email list string, subtracts one, and truncates the string to that shorter length. It then stuffs this new string into the 'Group Member Emails Trimmed' workflow attribute.

Lastly, I use the 'Send Email' action type and target my 'Group Member Emails Trimmed' attribute value for the 'Send To Email Address'.

  • Photo of Arran France

    0

    Hey Derek,

    Did this every get sorted? It'd be great to have this documented here.

    • Derek Mangrum

      I believe Version 4.0 will have this fixed. So, you will just be able to target a group and it will automatically send the email to all group members.

  • Photo of Derek Mangrum

    1

    As mentioned by Trey Hendon above, Line 152 (https://github.com/SparkDevNetwork/Rock/blob/07559604ee47d577f6f4de1eececaa61caae4697/Rock/Workflow/Action/SendEmail.cs) looks to evaluate the Workflow Attribute and process it if it is of type Group. After looking at this closely, this section of code only works if the group identifier is a Id (int). It is not able to handle the case where the group identifier is a Guid. This is where it was breaking in my case. For whatever reason, when my Attribute (of type Group) was being assigned a value from the 'Default Value' field, the group's Guid was being stored as that Attribute's value, not the group's Id. Nick Airdo confirmed, reported, and wrote a fix for this (https://github.com/SparkDevNetwork/Rock/issues/1167). So, we will see this in the next release.

  • Photo of Arran France

    0

    Is there a reason that 'assign to group' wouldn't work?

    • Derek Mangrum

      The 'Assign to Group' action doesn't have the ability to send a communication, as far as I can tell. I can (and do) assign Activities to Groups, but I need to be able to also send a notification email to all the group members.

    • Derek Mangrum

      Can you give more details on how you implemented this? I have an action that assigns an Activity to a Group, but I still don't see how to then send an email to all those group members. I would love to see how you are implementing that. I am getting lost on "User Entry Block" comment. I am not sure what that is and how it relates to Workflows/Activities/Actions. It sounds like you have a method that is much easier to implement than mine. I am eager to learn. Thank you!

    • Trey Hendon III

      Hey Derek,
      Line 152 (https://github.com/SparkDevNetwork/Rock/blob/07559604ee47d577f6f4de1eececaa61caae4697/Rock/Workflow/Action/SendEmail.cs) evaluates if the Attribute is a Group and gets all it's members to send an email.


      I really like this idea, though, and am trying similar (using SQL to build my Attribute string of comma separated emails). Unfortunately, though, for some reason, the emails aren't going out, while the Workflow Log shows successful. Did you have to do anything else in config to get the emails to work?


      -Trey

    • Trey Hendon III

      Sorry, should have clarified that more.


      For your case, you'll need an Attribute (a Group attribute) that gets set (or is defaulted to) the group you want to email.

    • Arran France

      Derek -
      What I did was use an 'User Entry' action which is simply a set of workflow attributes with some pre/post-HTML and then a command button which can trigger a new activity. The User Entry block can be configured to email a system email notification to a group that's assigned.


      For example, we're using this to assign newcomers to our visiting team. We're capturing data about the newcomer via SQL and storing it as workflow attributes (address, phone number), assigning the visiting team, and then the User Entry block is the next action. They get an email with the selected Workflow attributes (so we're showing them the name, address, and phone number but not some 'behind the scenes' attributes) and then a button at the bottom labelled 'Visited' to click which completes the workflow.


      The key part is the 'assign' action which is assigning the entire group which means the entire group is getting an email.


      Trey -
      What would be the advantage in a comma list versus a typical group?

  • Photo of Ken Roach

    0

    I don't know if this is the 'right' or the 'best' way to do this, but this is one way:

    Add a workflow attribute of type Group, let's say GroupToEmail.

    Add an action with Action Type of 'SQL Run.'  Use SQL like this: select guid from [group] where id=9383.  Where 9383 is the Group Id of the group you want to email to. 

    Add an action with Action Type of 'Email Send'.  Put 'GroupToEmail' into the 'Send To Email Addresses' in the Attribute Value drop down field (not the 'Send to Email Addresses' field).