Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

calling events details in notification email script

jobin1
Tera Expert

Hi All,

 

Below 2 blocks will be executed in a scheduled job and trigger the events, now in both blocks we have a set of different conditions and calling the events and both the block's event name is the same as below. now after this, we have a notification triggering based on the event fired and will send a notification via email script.

Now in the below notification email script how we can differentiate whether the event is triggered from which block of code i.e. //1st block or from //2nd block how this can be verified?

 

scheduled job.

//1st block

if (count2 > limit2)

gs.eventQueue('bpo.emailinjection.eml', gr, count, count2);
else {
gs.log("elseevent1");
gs.eventQueue('bpo.emailinjection', gr, count, count2);
}

 

//2nd block

if (count2 > limit2)

gs.eventQueue('bpo.emailinjection.eml', gr, count, count2);
else {
gs.log("elseevent2");
gs.eventQueue('bpo.emailinjection', gr, count, count2);
}

 

Notification email script.//sample 

(function runMailScript(current, template, email, email_action, event) {
 
    // Add your code here
 
    var instancename1 = gs.getProperty("instance_name");
var ritm = parseInt(event.parm1);
gs.log("ritm"+ritm);
    var attach = parseInt(event.parm2);
gs.log("attach"+attach);
    //Property name updated
    var limit = gs.getProperty('css_emailticket_threshold');
gs.log("limit"+limit);
    var limit2 = gs.getProperty('css_emailticket_emlthreshold');
gs.log("limit2"+limit2);
    email.setSubject("ALERT!! " + "\"" + instancename1 + "\"" + " instance - " + "Email ingestion monitoring");
 
attach = parseInt(attach);
gs.log("attach2"+attach);
limit2 = parseInt(limit2);
gs.log("limitsec"+limit2);
 
    //RITM count and .eml count messages updated_07202022
    if ((ritm < limit)) {
gs.log("1if");
if(attach > limit2){
gs.log("2if");
        template.print("Please review Email ingestion and Email Client records without .eml file on  " + "\"" + instancename1 + "\"" + " instance. Please check and take relevant actions as appropriate.");
 
        template.print("<br /> <br />");
//RITM count messages updated_09222022
        template.print("Total RITMs created or updated in last 1 hour: " + ritm + "  (Threshold limit per hour - " + limit + ")");
        template.print("<br /> <br />");
        template.print("Total Email Client records without .eml file in last 1 hour (refer attachment): " + attach);
}
}

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@jobin1 

send something unique from each block in event parameter and then use email script to check that

Something like this

//1st block

if (count2 > limit2)

gs.eventQueue('bpo.emailinjection.eml', gr, count, count2 + '@block1');
else {
gs.log("elseevent1");
gs.eventQueue('bpo.emailinjection', gr, count, count2);
}

 

//2nd block

if (count2 > limit2)

gs.eventQueue('bpo.emailinjection.eml', gr, count, count2 + '@block2');
else {
gs.log("elseevent2");
gs.eventQueue('bpo.emailinjection', gr, count, count2);
}

Email script

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

// Add your code here


var blockName = event.parm2.toString().split('@');

if(blockName == 'block1'){
// do something
}
else if(blockName == 'block2'){
// do something
}


var instancename1 = gs.getProperty("instance_name");
var ritm = parseInt(event.parm1);
gs.log("ritm"+ritm);
var attach = parseInt(event.parm2);
gs.log("attach"+attach);
//Property name updated
var limit = gs.getProperty('css_emailticket_threshold');
gs.log("limit"+limit);
var limit2 = gs.getProperty('css_emailticket_emlthreshold');
gs.log("limit2"+limit2);
email.setSubject("ALERT!! " + "\"" + instancename1 + "\"" + " instance - " + "Email ingestion monitoring");

attach = parseInt(attach);
gs.log("attach2"+attach);
limit2 = parseInt(limit2);
gs.log("limitsec"+limit2);

//RITM count and .eml count messages updated_07202022
if ((ritm < limit)) {
gs.log("1if");
if(attach > limit2){
gs.log("2if");
template.print("Please review Email ingestion and Email Client records without .eml file on " + "\"" + instancename1 + "\"" + " instance. Please check and take relevant actions as appropriate.");

template.print("<br /> <br />");
//RITM count messages updated_09222022
template.print("Total RITMs created or updated in last 1 hour: " + ritm + " (Threshold limit per hour - " + limit + ")");
template.print("<br /> <br />");
template.print("Total Email Client records without .eml file in last 1 hour (refer attachment): " + attach);
}
}

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

 

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

View solution in original post

5 REPLIES 5

I achieved it via email script but I need to pass the instance name in the subject how this can be done, body I did already like below

 

(function runMailScript(current, template, email, email_action, event) {
 
        var instancename1 = gs.getProperty("instance_name");
 
gs.log("testcountisstarting");
var event = new GlideRecord('sysevent');//new
event.addEncodedQuery('sys_created_onONToday@javascript&colon;gs.beginningOfToday()@javascript&colon;gs.endOfToday()^stateNOT INprocessed,error,transferred^processedISEMPTY');
event.query();
if(event.next())
gs.log("ifevent");
var count = event.getRowCount();//new
gs.log("testcountis"+count);
if (count >= 1) {
var count2 = event.getRowCount();//new
 
 
 
//
 
template.print(instancename1 + "Event processing is stuck please check.event count is" + count2);
}
 
 
 
})(current, template, email, email_action, event);