Question is:
How can I configure a notification rule so that the recipient is a person associated with the item – for example a Project Manager for the Project item type, or Requestor for the Requirement item type? A special rule type exists for sending a notification email to the person assigned to an item, but what about other types of persons?
Answer:
The way to do this is to add functionality to the Notification Rule Type so that the notification email recipient can be overridden in the same way that the message body and subject can. To accomplish this, edit the script for the Notification Rule Type as follows:
Locate the following code segment:
// Set up recipients as list
var orgEntries = new java.util.ArrayList();
for (var i = 0; i < recipients.length; i++)
orgEntries.add(recipients[i]);
And replace it with this:
// Set up recipients as list
var orgEntries = new java.util.ArrayList();
//*** Original code segment ************************************
// for (var i = 0; i < recipients.length; i++)
// orgEntries.add(recipients[i]);
//*** Replacement code segment *********************************
if (typeof recipOverride != "undefined") {
orgEntries.add(recipOverride);
} else {
for (var i = 0; i < recipients.length; i++)
orgEntries.add(recipients[i]);
}
//*** End of code change **************************************
Now that you have a way to override the recipient list for the notification email, you can make use of it when this is needed, but use the standard recipient list when that will work better.
Here is an example of a notification rule script for the Project item type that overrides the subject of the message, the body, and the recipient:
//*** Begin script ********************************************
var itemName = item.getName();
var status = item.getAttributes().getItemByAttributeTypeName("Status").getText();
var prjmgr = item.getAttributes().getItemByAttributeTypeName("Project Manager").getValue();
var prjmgrName = prjmgr.getName();
subjectOverride = "The item name is " + itemName;
bodyOverride = "The status is " + status + " and the project manager is " + prjmgrName;
recipOverride = prjmgr;
#EXECUTE_DEFAULT_SCRIPT#
//*** End script ********************************************