How to stop triggering same notification multiple times from scheduled script ?

Community Alums
Not applicable

Hi Everyone,
I need to stop triggering notification multiple time from scheduled job.

script:

var gr = new GlideRecord("cmdb_ci_business_app");
gr.addEncodedQuery('assigned_to.active=false^assigned_to.sys_updated_onONLast 7 days@javascript:gs.beginningOfLast7Days()@javascript:gs.endOfLast7Days()');
gr.query();
while (gr.next()) {
    gs.eventQueue('fa.business.app.accountable.owner', gr, gr.assigned_to.manager.email, gr.assigned_to);
}

var gr1 = new GlideRecord("cmdb_ci_service_discovered");
gr1.addEncodedQuery('assigned_to.active=false^assigned_to.sys_updated_onONLast 7 days@javascript:gs.beginningOfLast7Days()@javascript:gs.endOfLast7Days()');
gr1.query();
while (gr1.next()) {
    gs.eventQueue('fa.business.app.accountable.owner', gr1, gr1.assigned_to.manager.email, gr1.assigned_to);
}

 

Thanks @Ankur Bawiskar @Mohith Devatte 

find_real_file.png

1 ACCEPTED SOLUTION

@Venu gopal sr then do one thing you can push all those records  sys_id into an array and send that array as a parameter like below and use an email script in the notification vody to print the details of the business application 

Example:

1) I push assigned to and assigned to manager into array and sent as parameter1.In the notification check the check box recipient contains parm1 under whom to send

2)Then i push all the business app names into an array and sen as parameter 2

var bussinessArr=[];
var bussRecipients=[];
var gr = new GlideRecord("cmdb_ci_business_app");
gr.addEncodedQuery('assigned_to.active=false^assigned_to.sys_updated_onONLast 7 days@javascript:gs.beginningOfLast7Days()@javascript:gs.endOfLast7Days()');
gr.query();

while (gr.next()) {
bussinessArr.push(gr.name.toString());
bussRecipients.push(gr.assigned_to.manager.toString());
bussRecipients.push(gr.assigned_to.toString());
}
 gs.eventQueue('fa.business.app.accountable.owner', gr, bussRecipients , bussinessArr);

Now create a email script like below 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

         var busApps = event.parm2;
	template.print('please find the Business apps which are assinged to users whoc are incative under you');
	for(var i=0;i<busApps.length;i++)
		{
			template.print("Business App :"+i+" "+ busApps[i]);
		}

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

Then call this email script in your notification as ${mail_script:your_mail_Script_name}

this way you can send a single notification to inactive users  managers with multiple records 

Hope this helps 

please mark my answer correct if this helps you

 

View solution in original post

13 REPLIES 13

Thank you so much for your answer. I had the same problem. Scheduled job was triggering multiple emails and now I used gs.eventQueue outside the loop that helped.  Now it has stopped the multiple notifications.

I have provided the email in the users - whom to send in the notifications. Do I still need to check the parm1?  It is going to the correct person from the email.

Your answer was very helpful for me. Really appreciated you explaining in detail. 

Thank you

Shubha

Abhijit4
Mega Sage

This scheduled job will trigger notifications to all Inactive assigned to users for Business App and Service Discovered so what's issue exactly here?

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Community Alums
Not applicable

I need to send notification for inactive user's manager in assigned_to field where it can be multiple records on business application for same user, but I need to send only one notification to inactive users manager. but it's triggering multiple times because multiple of records. so, That's the issue I am facing here. 
Thanks

To help you further, I need to know what is the content of the notification. Does it include any field details from the Business Application or Service Discovered record?

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP