Creating your own workflow emails for tasks
I wanted to share this as another method of sending emails. This option has you turning off send notification on a workflow task, mine is called "CCB Review". In a workflow expression, I call out my helper class and method like so:
customization.workflow.MyWorkflowProcessHelper.sendNotifications((wt.workflow.engine.WfProcess)self.getObject(),"CCB Review");
This looks up all workitems that match that task name and creates its own email. They key here is the bolded lines in "createNotification". Here is where you can change the template processor AND the html template. I for other reasons chose not to but this opens up the possibility of completely customizing what users see in their email boxes. Hope this helps someone.
/**
* Fires notifications for an Assigned Activity where notification is turned off in the template.
* This allows modification of the activity before the emails are sent.
* @param process, reference to the current workflow process
* @param activityName, name of assigned activity
* @throws WTException
*/
public static void sendNotifications(WfProcess process, String activityName) throws WTException
{
LOGGER.debug("In sendNotifications");
WfAssignedActivity assignedActivity=getActivityByName(process, activityName);
if (assignedActivity!=null)
{
Enumeration assignments=assignedActivity.getAssignments();
LOGGER.debug("Retrieved Assignments");
WfAssignedActivityTemplate activityTemplate = (WfAssignedActivityTemplate)assignedActivity.getTemplate().getObject();
while (assignments.hasMoreElements())
{
WfAssignment assignment=(WfAssignment)assignments.nextElement();
if (!assignment.isComplete()) //process only if not complete
{
QueryResult result= PersistenceHelper.manager.navigate(assignment, "workItem", WorkItemLink.class, true);
LOGGER.debug("Returned " + result.size() + " for assignement");
while (result.hasMoreElements())
{
WorkItem workItem=(WorkItem)result.nextElement();
LOGGER.debug("Creating email");
TemplateEmailNotification email = createNotification(assignedActivity, workItem, workItem.getOwnership().getOwner());
LOGGER.debug("Sending Email to " + workItem.getOwnership().getOwner().getName());
NotificationHelper.manager.send(email);
}
}
}
}
}
private static TemplateEmailNotification createNotification(WfActivity activity, WorkItem workItem, WTPrincipalReference user) throws WTException
{
WTDistributionList distroList = new WTDistributionList();
Persistable p = null;
if ((workItem.getPrimaryBusinessObject() != null) && (workItem.getPrimaryBusinessObject().toString() != null))
p = workItem.getPrimaryBusinessObject().getObject();
if (AccessControlHelper.manager.hasAccess(user.getPrincipal(), p, AccessPermission.READ)) {
LOGGER.debug("adding " + user.getDisplayName() + " to distribution.");
distroList.addPrincipal(user.getPrincipal());
}
TemplateEmailNotification email = new TemplateEmailNotification(distroList);
Object[] arrayOfObject = new Object[5];
arrayOfObject[0] = activity.getName();
email.setSubjectResource("wt.workflow.work.workResource");
email.setSubjectMessageKey("124");
email.setSubjectInserts(arrayOfObject);
WfTaskNotificationProcessor processor = new WfTaskNotificationProcessor(workItem);
email.setTemplateProcessor(processor);
email.setTemplate(TemplateName.getWorkNotification("General"));
String str = "";
try {
str=WTProperties.getLocalProperties().getProperty("wt.notify.notificationSenderEmail");
}
catch (IOException e) {}
WTPrincipal localWTPrincipal = activity.getParentProcess().getCreator().getPrincipal();
if (localWTPrincipal instanceof WTUser) {
try {
str = new InternetAddress(((WTUser)localWTPrincipal).getEMail()).getAddress();
}
catch (NullPointerException nullE) {}
catch (Exception e)
{
LOGGER.debug("Exception while creating IntenetAddress==>");
}
LOGGER.debug("Sender:" + str);
}
email.setSender(str);
LOGGER.debug("Created email, returning.");
return email;
}
private static WfAssignedActivity getActivityByName(WfProcess process, String activityName) throws WTException
{
LOGGER.debug("In getApprovalAssignment()");
QuerySpec querySpec= new QuerySpec(WfAssignedActivity.class);
SearchCondition condition1=new SearchCondition(WfAssignedActivity.class, "parentProcessRef.key", SearchCondition.EQUAL, PersistenceHelper.getObjectIdentifier(process));
querySpec.appendWhere(condition1, 0);
QueryResult result=PersistenceServerHelper.manager.query(querySpec);
LOGGER.debug("Found " + result.size() + " activities related to process.");
while (result.hasMoreElements())
{
WfAssignedActivity tempActivity=(WfAssignedActivity)(result.nextElement());
LOGGER.debug("Activity Name: " + tempActivity.getName());
if (tempActivity.getName().equals(activityName))
return tempActivity;
}
return null;
}


