cancel
Showing results for 
Search instead for 
Did you mean: 

How to send mail to all users of a certain group

f_lombardo
Champ in-the-making
Champ in-the-making
Hi all,

I'm looking for a little suggestion about a process I'm designing.
I'm trying to create a mail task that sends an email to all users of a certain group.

I'd like to write an expression like this:

<serviceTask id="mailtaskXX" name="MailXX" activiti:type="mail">
      <extensionElements>
          <activiti:field name="to" expression="${mySmartObject.getAddressesForGroup("myFunnyGroup")}/>

But I'm not smart enough to write mySmartObject! 🙂 This object should connect to the IdentityService of the Activiti Engine in which the process is running and query it to obtain all the addresses for the group it receives as a parameter.

Could you help me?

Thank you very much!!!

Bye

Franco
4 REPLIES 4

jbarrez
Star Contributor
Star Contributor
What exactly is blocking you? The smartObject is just a Java bean that has access to the identityService, which is used to build up the return value. If you are using Spring, you can autowire this bean with the identityService. Otherwise, you'd need to use for example the ProcessEngines.getDefaultEngine().getIdentityService() class.

f_lombardo
Champ in-the-making
Champ in-the-making
ProcessEngines.getDefaultEngine().getIdentityService()

Oh, that's the magic line of code I was missing, thanks!!! 🙂

Now, would it be possible to invoke to a static method, like


    <serviceTask id="mailtask01" name="Mail01" activiti:type="mail">
      <extensionElements>
        <activiti:field name="to" expression="${my.package.MailUtil.mailForGroup("myFunnyGroup")}"></activiti:field>

I tried, but I got an exception:


org.activiti.engine.impl.javax.el.PropertyNotFoundException: Cannot resolve identifier 'my'
        at org.activiti.engine.impl.juel.AstIdentifier.eval(AstIdentifier.java:83)

Thanks again.

Bye

Franco

f_lombardo
Champ in-the-making
Champ in-the-making
Well, I answer to myself….

I created a simple POJO like this:


public class MyMailUtil {

  public String mailForGroup(String groupId) {
    StringBuffer result = new StringBuffer();
    List<User> users = identityService().createUserQuery().memberOfGroup(groupId).list();
    for (User user : users) {
      if (result.length() > 0) {
        result.append(", ");
      }
      result.append(user.getEmail());
    }
    return result.toString();
  }

  protected IdentityService identityService() {
    return ProcessEngines.getDefaultProcessEngine().getIdentityService();
  }
}

I deployed it in a jar file in the lib directory of the tomcat webapp I use (explorer, rest…)

Then at the end of the spring .xml config file I added this line


  <bean id="myMailUtil" class="my.package.MyMailUtil" />

Finally, I created a mail task this way:


    <serviceTask id="mailtask01" name="Mail" activiti:async="true" activiti:type="mail">
      <extensionElements>
        <activiti:field name="to" expression="#{myMailUtil.mailForGroup("myFunnyGroup")}"></activiti:field>

…and it works….Activiti is great!

Bye

Franco

jbarrez
Star Contributor
Star Contributor
That is indeed the way to do it! I'm happy you found out yourself how easy it is 🙂

Thanks for following up!