Set notification recipients dynamically

rez4
Kilo Guru

I Created an email script to add the caller of the incident to the notification recipients but when I call it in the body of the notification nothing happens.

Here is the script

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
// && current.caller_id != current.u_on_behalf_of
    if (!current.caller_id.nil()&& current.caller_id != current.u_on_behalf_of) {
        //get caller addresses and add to cc
        //get user records
        var user = new GlideRecord("sys_user");
        user.addQuery("sys_id", current.caller_id);
        user.addQuery("notification", 2);
        //email
        user.addQuery("email", "!=", "");
        user.query();

        while (user.next()) {
            //add to cc list    
            email.addAddress("cc", user.email, user.getDisplayValue());
        }
    }

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

 

Is there a way to get this to work or I need to go and create an event, a business rule, and use event parm

2 REPLIES 2

Muhammad Khan
Mega Sage
Mega Sage

Hi rez,

 

Try replacing

 email.addAddress("cc", user.email, user.getDisplayValue());

with

 email.addAddress("cc", user.email.toString(), user.getDisplayValue());

 

Hopefully, this will resolve your query.

Harish KM
Kilo Patron
Kilo Patron

Try changing 

email.addAddress("cc", user.email, user.getDisplayValue());

to

email.addAddress("cc", user.getValue('email'), user.getDisplayValue());

also add a log inside while loop to test whether code is running

Regards
Harish