cancel
Showing results for 
Search instead for 
Did you mean: 

Assignee NodeRef in Workflow Email Template?

cpaul
Champ on-the-rise
Champ on-the-rise
Hello all,

I'm attempting to update the workflow assignment notification email so that the assignee's name is in the greeting. To do this, I will modify the "wf-email.html.ftl" template in "/Company Home/Data Dictionary/Email Templates/Workflow Notification".

I see that there is a "person" argument available that is set up by org.alfresco.repo.action.executer.MailActionExecuter.createEmailTemplateModel() (line 673). I can use this argument in the FreeMarker template to reference the assigner NodeRef properties like so:

${person.properties["cm:firstName"]} ${person.properties["cm:lastName"]}
I'd like to do the same thing using the assignee user NodeRef, but this doesn't appear to be available in the template. Looking at the code, it seems I'll have to extend MailActionExecuter and override the createEmailTemplateModel method so that I can inject this argument for the template, and then override the Spring bean to wire it in.

Is this the correct approach? It seems like there should be a simpler way to do this?

Thanks!
Chris
2 REPLIES 2

cpaul
Champ on-the-rise
Champ on-the-rise
Ok, I figured out an approach that works, but am still open to anything simpler!

I extended org.alfresco.repo.notification.EMailNotificationProvider and overrode the following method:

@Override
public void sendNotification(NotificationContext notificationContext)
{
   List<String> to = notificationContext.getTo();
   if (to != null && to.size() == 1) {
      String assignee = to.get(0);
      Map<String, Serializable> args = notificationContext.getTemplateArgs();
      try {
         NodeRef personRef = serviceRegistry.getPersonService().getPerson(assignee, false);
         args.put("assignee", new TemplateNode(personRef, serviceRegistry, null));
         notificationContext.setTemplateArgs(args);
      } catch (NoSuchPersonException ex) {
         logger.warn("Could not find a person with the name " + assignee);
      }
   }
   super.sendNotification(notificationContext);
}
Then I overrode the "emailNotificationProvider" in my custom context file, using this new class.

Finally, at the top of the wf_email.html.ftl file, I added:

<#assign assignee=args["assignee"]/>
Now I'm able to use the assignee user as follows:

<#if assignee?exists>
${assignee.properties["cm:firstName"]} ${assignee.properties["cm:lastName"]}
</#if>

kachaj7
Champ in-the-making
Champ in-the-making

Missing code how was implemented serviceRegistry

package PACKAGE_NAME; // replace with own package name.

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.alfresco.repo.template.TemplateNode;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.notification.NotificationContext;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.security.NoSuchPersonException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class EMailNotificationProvider extends org.alfresco.repo.notification.EMailNotificationProvider {
    private static Log logger = LogFactory.getLog(EMailNotificationProvider.class);

    private ServiceRegistry serviceRegistry;

    @Override
    public void sendNotification(NotificationContext notificationContext) {
        List<String> to = notificationContext.getTo();
        if (to != null && to.size() == 1) {
            String assignee = to.get(0);
            Map<String, Serializable> args = notificationContext.getTemplateArgs();
            try {
                NodeRef personRef = this.serviceRegistry.getPersonService().getPerson(assignee, false);
                args.put("assignee", new TemplateNode(personRef, this.serviceRegistry, null));
                notificationContext.setTemplateArgs(args);
            } catch (NoSuchPersonException ex) {
                logger.error("Could not find a person with the name " + assignee);
            } catch (Exception ex){
                logger.error("Exception: " + ex.getMessage());
            }
        }
        super.sendNotification(notificationContext);
    }

    /**
     * @param serviceRegistry provides access to the service APIs
     */
    public void setServiceRegistry(ServiceRegistry serviceRegistry)
    {
        this.serviceRegistry = serviceRegistry;
    }    
}

Missing example of bean definition in file module-context.xml

 <!--  EMail notification provider, source https://github.com/Alfresco/alfresco-repository/blob/master/src/main/resources/alfresco/notification-services-context.xml -->
    <bean id="emailNotificationProvider" class="PACKAGE_NAME.EMailNotificationProvider" init-method="init">
        <property name="notificationService" ref="notificationService"/>
        <property name="nodeService" ref="NodeService"/>
        <property name="actionService" ref="ActionService"/>
        <property name="personService" ref="PersonService"/>
        <property name="repository" ref="repositoryHelper"/>
        <property name="fileFolderService" ref="FileFolderService"/>
        <property name="repoAdminService" ref="RepoAdminService"/>
        <property name="serviceRegistry" ref="ServiceRegistry" />        
    </bean>