Send Email from a Workflow

Generally emails are sent from a workflow to a user to escalate the importance of a task as it moves during the transition from one state to another. There are a number of reasons why a workflow might be setup to do this. One reason might be that a task was rejected from a state by the 'reviewer' due to the task not being done correctly. In this case, if there was a business action set up on the transition to notify the previous user, the user then would receive an email saying that the task they had previously pushed through was sent back. No matter the reason for setting up email notifications from workflows, it is possible to set this configuration up via a business action utilizing the "Mail Home" bind. The business action can be configured or used on the State Editor screen in the 'Deadline/Escalation', 'On Entry', or 'On Exit' tabs, as well as on the Transition Editor on the 'On Transition' tab.

For information on configuring email from STEP, refer to the Email from STEP topic in the Resource Materials online help documentation.

Dynamic Email Addresses

When sending emails from a workflow, it is often necessary to send emails to different users who interact with the same workflow. One scenario for this might be that a number of users may claim tasks from the same state. If the task is later released, and then rejected due to an error, it is important to inform the correct user who last worked on it.

Configuring Emails from Workflows

The script example below shows how an email address defined on a STEP User can be obtained, and how an email without attachments is sent to that user. In the following example, this script is used on a Transition Editor on the 'On Transition' tab.

var userId = node.getWorkflowInstance(workflow).getSimpleVariable("VariableHoldingUserID");
var emailAddress = step.getUserHome().getUserById(userId).getEMail();
if(emailAddress) {
   mail.send("noreply@stibo.com", emailAddress, "From Workflow", 
   "This is a mail sent from a Workflow");
}
else {
   logger.warning("No email address specified for User with ID " + userId);
}

Conditions

To execute the above configuration, the following two conditions must be met for 'Sending Email from a Workflow'.

  1. A Simple Mail Transfer Protocol (SMTP) server must be configured in the executing system.
  2. The user who is to receive the email should be entered into the User tab under description. To do so, go to System Setup, navigate to the node that houses Users and Groups, expand the node and click on the user who will be emailed. Enter in their name and email.

In the example below, there are two business actions set up to send emails to various users.

One business rule is on the State Editor on the 'On Exit' tab coming from the Edit state going to the Review state. This business action, which is pictured below, is set up to retrieve the email ID of a user who submits a task from Edit state to the Review state as a variable. In this case, 'Released_by' is the variable defined to retrieve the email ID.

Refer to Workflow Variables in the Workflows documentation for more on workflow variables here.

The second business action is on the Transition Editor on the 'On Transition' tab. This transition is coming from the Review state going to the Edit state and has the event name Reject.

The business action script uses the variable 'Released_by' as well, where it is helping to identify who to send the workflow email to and notify that the task they just submitted was rejected. This script also is sending the email to the user without any attachments other than the message. Notice that the message shown in pink, can be crafted to say anything that might be helpful to the receiver whenever a task is rejected.

It is important to note that if there is no email address registered, or if the email is not a valid one, the executing user is not notified (e.g., no pop-up appears). However, it is captured in the STEP system log on the application server.

If the user doing the rejecting should be notified that no email was sent and provided with an error message, then a business condition must be added to the Transition Editor on the 'Condition' tab. The following script is what should be entered in below.

var userId = node.getWorkflowInstance(workflow).getSimpleVariable("Released_by");
var emailAddress = step.getUserHome().getUserById(userId).getEMail();
if(emailAddress) {
   return true;
}
else {
   return "No email address specified for User with ID " + userId;
}

Static Email Address

In the above scenario, STEP will trigger emails to the executing users. However, in some cases the same user will always need to be notified and never change (a static email address). In this case it is possible to send emails to the static email address with the 'Send Email' operation, and no JavaScript is needed.

This 'Send Email' operation can be added in a State Editor on the 'On Entry', 'On Exit' or 'Escalation' tabs. It can also be sent in the Transition Editor on the 'On Transition' tab. Select the appropriate Editor and tab and then select Add new Business Action > Merge into > Send Email.