Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to send email grouped by Requested for.. for requests/ritms referencing the number to the url

Vishal Jawalka1
Tera Expert

This Article guides you on how to Send out emails to Requested for ( users ) based on how many active Requests/RITMs they have part of.

Step 1:

 

1. create a schedule job, or BR based on the requirement, use the below code:

var userMap = {}; // userID → array of RITM objects

// Step 1: Collect RITMs grouped by u_requested_for with sys_id, number, and short_description
var gr = new GlideRecord('enter table');
gr.addEncodedQuery(use your code');
gr.query();

while (gr.next()) {
var userId = gr.requested_for.toString();
if (!userMap[userId]) {
userMap[userId] = [];
}
userMap[userId].push({
number: gr.number.toString(),
sys_id: gr.sys_id.toString(),
short_description: gr.short_description.toString()
});
}

// Step 2: Build HTML table and send event per user
for (var userId in userMap) {
var userGR = new GlideRecord('sys_user');
if (userGR.get(userId)) {
var email = userGR.getValue('email');
var name = userGR.getValue('name');

// Build HTML table with clickable links and short descriptions
var yourtable = '<table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse;">';
yourtable+= '<tr><th>table</th><th>Short Description</th></tr>';

userMap[userId].forEach(function(ritm) {
var url = gs.getProperty('glide.servlet.uri') + 'yourtable.do?sys_id=' + yourtable.sys_id;
yourtable+= '<tr>';
yourtable+= '<td><a href="' + url + '" target="_blank">' + yourtable.number + '</a></td>';
yourtable+= '<td>' + yourtable.short_description + '</td>';
yourtable+= '</tr>';
});

yourtable += '</table>';

if (email) {
gs.print('Sending notification to: ' + name + ' (' + email + ')');
gs.print('yourtable table: ' + yourtable);

// Fire event with HTML table as parm1 and email as parm2
gs.eventQueue('yourevent', userGR, yourtable, email);
} else {
gs.warn('No email found for user: ' + name);
}
} else {
gs.warn('Could not find user for ID: ' + userId);
}
}

2. Create your event ( name it format it)

3. Create your notification on User table, please check use parm1 and parm2 ( as they are used for reference to send emails).

 

you can execute your script daily using scheduled script, or based on a DB action ( create, read, update, delete)

 

0 REPLIES 0