cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

We are happy to announce the new Windchill Customization board! Learn more.

Creating your own workflow emails for tasks

avillanueva
22-Sapphire I

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;

}

1 ACCEPTED SOLUTION

Accepted Solutions

avillanueva_0-1639571579264.png

It may not need to be as complex as my example but from the "Choose CCB Team" step, when they send for signoff, it goes in many directions. The "CCB Review" task does not have send notifications checked. My send occurs from the conditional towards the top but you could just as easily do this from a transition on the task itself. It would not work in my case for other reasons. 

So in the conditional, I call:

customization.workflow.MyWorkflowProcessHelper.sendNotifications((wt.workflow.engine.WfProcess)self.getObject(),"CCB Review");

This should find all the task instances (WorkItems) with that name at this step and then send a email. You'll need to create your own help class (best practice to externalize code in workflows unless its dirt simple).

View solution in original post

3 REPLIES 3

hello avillanueva, it's a really detailed study and it will probably be useful for many people.

I also create workflows and send emails to users. I think it will work for me too, but I don't understand how to use it exactly.

Should I write the code in the activity screen? Or should I write it somewhere else?

 

If you put a screenshot on the subject, you can make me the happiest person in the world. Thank you very much, best regards.

avillanueva_0-1639571579264.png

It may not need to be as complex as my example but from the "Choose CCB Team" step, when they send for signoff, it goes in many directions. The "CCB Review" task does not have send notifications checked. My send occurs from the conditional towards the top but you could just as easily do this from a transition on the task itself. It would not work in my case for other reasons. 

So in the conditional, I call:

customization.workflow.MyWorkflowProcessHelper.sendNotifications((wt.workflow.engine.WfProcess)self.getObject(),"CCB Review");

This should find all the task instances (WorkItems) with that name at this step and then send a email. You'll need to create your own help class (best practice to externalize code in workflows unless its dirt simple).

I'll try it in one of my workflows. thank you very much. 

Top Tags