Pull watch list emails script

Thomas G
Tera Guru

Hi,

In an Email Client Template that has the incident_alert_task table set, I need to call an email mail script that pulls email(s) from members on the source incident watch list. I have tried a lot, but nothing seems to work. The following script should work, as far as I can see, but it is not. Can you figure out why?

Mail script 'getWatchListFromIncident':

 

(function() {
    var incidentGR = new GlideRecord('incident');
    
    // Get the incident ID from the source_incident reference in the current task
    var incidentId = current.incident_alert.source_incident;
    
    // Ensure the incident exists
    if (incidentId && incidentGR.get(incidentId)) {
        var emails = [];
        var watchList = incidentGR.watch_list;  // GlideList field
        
        // Iterate over each user in the watch list
        watchList.forEach(function(user) {
            var userGR = new GlideRecord('sys_user');
            if (userGR.get(user) && userGR.email) {
                emails.push(userGR.email);  // Add user email to the list
            }
        });

        // Return the emails as a comma-separated string
        return emails.join(', ');
    }
    
    return "";  // Return an empty string if no emails found or incident is invalid
})();

 

In the Cc field on the Email Client Template, I call the script via:

 

${mail_script:getWatchListFromIncident} 

 

but it does not pull emails from members on the watch list of the source incident

Best regards
Thomas

1 ACCEPTED SOLUTION

and this line we have to put in the actual email client template 

 

javascript: new getWatchListFromIncident().getemails(current.incident_alert.source_incident);

Please go to email client template and open the email client template you are using to compose and in that Recipients section of CC or BCC field try to add this line and check once.

RameshPoola_0-1728030642899.png

 


 Regards,
Ramesh

View solution in original post

14 REPLIES 14

Hi Ramesh,

This is what I have right now:

Mail script 'getWatchListFromIncident':

(function() {
    var incidentGR = new GlideRecord('incident');
    
    // Get the incident ID from the source_incident reference in the current task
    var incidentId = current.incident_alert.source_incident;
    
    // Ensure the incident exists
    if (incidentId && incidentGR.get(incidentId)) {
        var emails = [];
        var watchList = incidentGR.watch_list.split(',');  // GlideList field
        
        // Iterate over each user in the watch list
        watchList.forEach(function(user) {
            var userGR = new GlideRecord('sys_user');
            if (userGR.get(user) && userGR.email) {
                emails.push(userGR.email);  // Add user email to the list
            }
        });

        // Return the emails as a comma-separated string
        return emails.join(', ');
    }
    
    return "";  // Return an empty string if no emails found or incident is invalid
})();

To test it, I go to an Incident Communication Task record (on the incident_alert_task table). It has an incident number in the source_incident-field and that incident has two users in the watch_list field.

I then click on the 'Compose' related link on the ICT-record and in the Cc and/or Bcc fields I enter:

${mail_script:getWatchListFromIncident} 

Then I press 'Send'. 

When inspecting the email in the sys_email table, no emails are added to Cc or Bcc fields. Actually those fields are not even visible in the Headers section. I guess the reason is that they are empty(?).

Best regards
Thomas

Hi @Thomas G 

You can check copied and Blind Copied Field on the Email record for CC and BCC Values.

VoonaRohila_0-1728023876597.png

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Thanks, Voona,

I see that Copied and/or Blind copied values are empty when calling the email script from those fields, alas

Best regards
Thomas

Hi Thomas,

I don't think we can call email notification script there, Instead I would suggest you to please include this code in script include or create a new script include and call it. It should work, I was following this method only for email client.

Please try once and let me know.

Best Regards,
Ramesh

to call script include syntax is :
javascript: new APIName.ScriptIncludeName().FunctionName(

current.incident_alert.source_incident);
 
the script will be : 

 

 

getemails : function(incId) {
    var incidentGR = new GlideRecord('incident');
    
    // Get the incident ID from the source_incident reference in the current task
    var incidentId= incId ;
    
    // Ensure the incident exists
    if (incidentId && incidentGR.get(incidentId)) {
        var emails = [];
        var watchList = incidentGR.watch_list;  // GlideList field
        
        // Iterate over each user in the watch list
        watchList.forEach(function(user) {
            var userGR = new GlideRecord('sys_user');
            if (userGR.get(user) && userGR.email) {
                emails.push(userGR.email);  // Add user email to the list
            }
        });

        // Return the emails as a comma-separated string
        return emails.join(', ');
    }
    
    return "";  // Return an empty string if no emails found or incident is invalid
},