Service catalog email notification

abhisek
Tera Contributor

My requirement is related to all Service Catalogue email notifications to start with a personalized greeting.

Some notifications are getting triggered by insert or update, some are by event. In some cases, the recipient of the notification is requested for or opened by or watch list, in some cases, the recipients of the notification is requested for and opened by and watch list and event creator (send to event creator checkbox is checked) as well. In some cases, event parm 1 or event parm 2 or both are checked. The requirement is if the recipient is only 1 user, then personalized greeting should be Dear Recipient Full Name but if the recipients are more than 1 then personalized greeting should be Dear Customer. But if both the recipients are same user for example Requested For and Watchlist are same user then personalized greeting should be Dear Recipient Full Name if not same then Dear Customer. Could you please let me know how ca I achieve it? It is bit urgent. Thanks in advance.

3 REPLIES 3

vaishali231
Kilo Sage

Hey @abhisek  

Try this :

Create a Mail Script

(function runMailScript(current, template, email, email_action, event) {

   var recipients = {};

   // Get all recipient email addresses
   var recipientList = email.getAddress("to");
   if (recipientList) {
       var emails = recipientList.split(",");
       for (var i = 0; i < emails.length; i++) {
           var addr = emails[i].trim().toLowerCase();
           if (addr)
               recipients[addr] = true;
       }
   }

   var uniqueEmails = Object.keys(recipients);
   if (uniqueEmails.length == 1) {
       var user = new GlideRecord('sys_user');
       user.addQuery('email', uniqueEmails[0]);
       user.query();
       if (user.next()) {
           template.print("Dear " + user.getDisplayValue() + ",<br/><br/>");
       } else {
           template.print("Dear Customer,<br/><br/>");
       }
   } else {
       template.print("Dear Customer,<br/><br/>");

   }

})(current, template, email, email_action, event);

 

 Use it in every Notification

At the beginning of the email body add

${mail_script: Name of Email Script }

 

*********************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb









Hi @vaishali231 , Thanks for your time and reply. I would like to let you know that it is not working.

 

Regards,

Abhisek Chattaraj

Tanushree Maiti
Tera Patron

Hi @abhisek  

 

1. Try with this email script:

  • Navigate to System Policy > Email > Notification Email Scripts >Click New.
  • Name: email_dynamic_greeting

 

(function runMailScript(current, template, email, event, email_action) {

    var recipients = [];

    if (current.isValidRecord()) {

        if (current.requested_for) recipients.push(current.requested_for.toString());

        if (current.opened_by) recipients.push(current.opened_by.toString());

        if (current.watch_list) {

            var watchList = current.watch_list.toString().split(',');

            for (var i = 0; i < watchList.length; i++) {

                if (watchList[i]) recipients.push(watchList[i].toString());

            }

        }

    } else {

        if (event.parm1) {recipients.push(event.parm1.toString());}

        if (event.parm2) { recipients.push(event.parm2.toString());}

    }

    var uniqueRecipients = [];

    for (var k = 0; k < recipients.length; k++) {

        if (uniqueRecipients.indexOf(recipients[k]) === -1) {

            uniqueRecipients.push(recipients[k]);

        }

    }

    if (uniqueRecipients.length === 1) {

        var userReceipentGr = new GlideRecord('sys_user');

        if (userReceipentGr.get(uniqueRecipients[0])) {

            template.print("Dear " + userReceipentGr.name.toString() + ",<br/><br/>");

        } else {

            template.print("Dear Customer,<br/><br/>");

        }

    } else {

        template.print("Dear Customer,<br/><br/>");

    }

})(current, template, email, event, email_action);

 

2: Add the Script to Your Notifications

  • Open your Service Catalogue Notification record.
  • In the What it contains tab, clear out any static greetings
  • Call the email script using the following syntax:
    ${mail_script:email_dynamic_greeting}

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti