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,

I get a 'Could not save record because of a compile error: JavaScript parse error at line (25) column (2) problem = syntax error (<refname>; line 25)' when trying to save your code as a new Script Include, so I have changed it a bit.

Name: getWatchListFromIncident:

var getWatchListFromIncident = Class.create();
getWatchListFromIncident.prototype = {
    initialize: function() {
    },

    getemails: function(incId) {
        var incidentGR = new GlideRecord('incident');
        
        // Get the incident ID from the parameter
        if (incidentGR.get(incId)) {
            var emails = [];
            var watchList = incidentGR.watch_list;  // GlideList field
            
            // Split watch_list by comma to handle the GlideList
            var watchListArray = watchList.split(',');

            // Iterate over each user in the watch list
            watchListArray.forEach(function(user) {
                var userGR = new GlideRecord('sys_user');
                if (userGR.get(user.trim()) && userGR.email) {  // Ensure user and email exist
                    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
    },

    type: 'getWatchListFromIncident'  // Ensures the type is set correctly
};


Then from the 'Compose' related link on the ICT-record in the Cc field I enter:

javascript&colon; new getWatchListFromIncident().getemails(current.incident_alert.source_incident);

But no emails ends in the Cc field on the sys_email record. Source incident contains two users in the watch_list field.

Best regards
Thomas

Hi Thomas,

Could you please put some logs and check.

Regards,
Ramesh

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

 

javascript&colon; 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

Hi Ramesh,

Ahh, I see! It do indeed work, if I call the Script Include from the Cc field on the Email Client Template. Thank you very much for the help and patience! 😉

Best regards
Thomas

Hi Thomas,

 

Sounds good, Thanks for marking the  solution as helpful.

 

Best Regards,

Ramesh