Email script

Sironi
Kilo Sage

Hi,

someone help on incident script,

send  incident details list to assigned to if incident state is OnHold on single notification .

ex: DavidLoo hasbeen assigned to 10-incidents , in 10-incidents there are 7 incidents have state -onhold. so DavidLoo should receive 7-incident details (number, shortdescription, caller , priority)  in a single email.

i tried with below script, it is getting single incident details, how to get all in single email.

template.print("number :"+"${number}\n");

template.print("number :"+"${priority}\n");

template.print("number :"+"${caler_id}\n");

template.print("number :"+"${short_description}\n");

 

1 ACCEPTED SOLUTION

Hi,

I just updated the job code

It triggered 3 emails to 3 different users

Email triggered to David Loo has incident info Assigned to David Loo

Email triggered to Rosie Mathews has incident info Assigned to Rosie Mathews

Email triggered to Fred Luddy has incident info Assigned to Fred Luddy

Now the only thing you need to check is why it didn't send email to other 6 users

Updated Job Code:

sendEmail();

function sendEmail() {

	var gr = new GlideAggregate("incident");
	gr.addAggregate("COUNT");
	gr.addEncodedQuery('active=true^assigned_toISNOTEMPTY');
	gr.groupBy("assigned_to");
	gr.query();
	while(gr.next()) {
		var user = gr.assigned_to;
		var incRec = new GlideRecord('incident');
		incRec.get('assigned_to', user);
		gs.eventQueue('test_list', incRec, incRec.assigned_to, incRec.assigned_to);
	}
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

46 REPLIES 46

@Sironi 

please try below updated script to get the details in more user friendly format in email body in tabular format

(function runMailScript(current, template, email, email_action, event) {

	// Add your code here

template.print("<table width=70% border=1><tr bgcolor=#e2e0de><td>Number</td><td>Priority</td><td>Caller ID</td><td>Short Desc</td></tr>");

var inc = new GlideRecord('incident');
inc.addQuery('assigned_to', current.assigned_to);
inc.addQuery('state','5');
inc.query();
while (inc.next()) {
template.print("<tr><td>inc.number</td><td>inc.priority</td><td>inc.caller_id.getDisplayValue()</td><td>inc.short_description</td></tr>")
}

template.print('</table>');

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

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Sorry to saythis, it is not working...

 

fred.luddy hasbeen assigned to 3-incidents, so 3-emails triggered each-email contain single record data 3times.

find_real_file.png

find_real_file.png

find_real_file.png

Hi,

if you require single email with 3 incident details then you need to trigger the eventQueue only once per user

In the email script you can have the query to show those 3 incident details

please find updated email script

(function runMailScript(current, template, email, email_action, event) {

	// Add your code here

template.print("<table width=70% border=1><tr bgcolor=#e2e0de><td>Number</td><td>Priority</td><td>Caller ID</td><td>Short Desc</td></tr>");

var inc = new GlideRecord('incident');
inc.addQuery('assigned_to', current.assigned_to);
inc.addQuery('state','5');
inc.query();
while (inc.next()) {
template.print("<tr><td>" + inc.number + "</td><td>" + inc.priority + "</td><td>" + inc.caller_id.getDisplayValue() + "</td><td>" + inc.short_description + "</td></tr>")
}

template.print('</table>');

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

Updated scheduled job script: use if instead of while

sendEmail();

function sendEmail(){

var userSysId = ''; // give here the user sys_id of david loo

var gr = new GlideRecord('incident');

gr.addEncodedQuery('active=true^assigned_to=' + userSysId);

gr.query();

if(gr.next()){

gs.eventQueue('test-list', gr, gs.getUserID(), gs.getUserName());

}

}

Regards
Ankur

 

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

 

Scheduled Job:

sendEmail();
function sendEmail(){
var userSysId = '5137153cc611227c000bbd1bd8cd2005'; // give here the user sys_id of david loo
var gr = new GlideRecord('incident');
gr.addEncodedQuery('active=true^assigned_to=' + userSysId);
gr.query();
if(gr.next()){
gs.eventQueue('test-list', gr, gs.getUserID(), gs.getUserName());

}
}

 

Emai-script:

(function runMailScript(current, template, email, email_action, event) {

// Add your code here

template.print("<table width=70% border=1><tr bgcolor=#e2e0de><td>Number</td><td>Priority</td><td>Caller ID</td><td>Short Desc</td></tr>");

var inc = new GlideRecord('incident');
inc.addQuery('assigned_to', current.assigned_to);
inc.addQuery('state','5');
inc.query();
while (inc.next()) {
template.print("<tr><td>" + inc.number + "</td><td>" + inc.priority + "</td><td>" + inc.caller_id.getDisplayValue() + "</td><td>" + inc.short_description + "</td></tr>");
}

template.print('</table>');

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

 

it is working now...

how to repeat for all assigned to users???