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

Ramesh Poola
Tera Guru

Hi Thomas,

the issue lies in this line. 

var watchList = incidentGR.watch_list;  // GlideList field

 

this returns the comma separated string value.

 

so to perform iteration operation we have to convert that to list, so Please update the above line with the below line and check once.

var watchList = incidentGR.watch_list.split(',');  // GlideList field

 

 

Please mark my answer helpful if its resolves your issue.


Best Regards,
Ramesh

Mark Manders
Mega Patron

You may need to add some logging to see if you are reaching the correct tables and fields, but can you try it like this:

(function() {
    var incidentGR = new GlideRecord('incident');
    var incidentId = current.incident_alert.source_incident;  // Ensure this dot-walk is correct
    
    if (incidentId && incidentGR.get(incidentId)) {
        var emails = [];
        var watchList = incidentGR.getValue('watch_list');  // gets comma separated sysID's
        
        if (watchList) {
            var watchListArray = watchList.split(',');  // Split the sys_ids into an array
            
            // Iterate over each user sys_id in the watch list
            for (var i = 0; i < watchListArray.length; i++) {
                var userSysId = watchListArray[i].trim();
                var userGR = new GlideRecord('sys_user');
                if (userSysId && userGR.get(userSysId) && userGR.email) {
                    emails.push(userGR.email);
                }
            }
        }

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

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Thomas G
Tera Guru

Thanks Ramesh and Mark,

Tried both but it does not work either. No emails are pulled from watch_list on the source incident and I am pretty sure that the dot-walk from incident_alert.source_incident is correct. I am also sure that the source incident has users on watch_list. 

I will try some logging.

Best regards
Thomas

Hi Thomas,

 

I used the code you shared, for me also initially it was not worked then I added this line

 

var watchList = incidentGR.watch_list.split(','); // GlideList field

 

Could you please confirm you added above line to your code or not. If you don't mind please share the code once again.

 

Best Regards,

Ramesh