Query for all records assigned to a user but send them one notification for all records

Michael Lee1
Tera Expert

Hoping someone can lend a little logic to my situation. 

I'm currently defining an SLA which will run on the RITM table. At the SLA milestones I'd like to send notifications to those who have an active SCTASK associated with that particular RITM, assigned to them. The problem that I'm running into is when one user has multiple SCTASK records assigned to them. So let's say for an onboard, one user may have 3 tasks while another has 2. I'd like to NOT send that one user a notification for each record and instead only send them one. My notification email script actually queries and prints a list of all active tasks related to the RITM, along with who they're assigned to. So each user only needs one notification regardless of if they have 3 SCTASK records assigned to them. 

I've included my current Run Script and my Notification Script. I believe what I need to do is query for each active record on the Run Script, but only trigger an event once for each user. I just haven't been able to figure out how to do that nor do I know if it would be the best way to do it. Any help would be greatly appreciated!

Workflow Run Script:

var notify = new GlideRecord('sc_task');
notify.addQuery('request_item', current.task.sys_id);
notify.addQuery('active', true);
notify.query();

while(notify.next()){
  gs.eventQueue('sla.warning.50',current,current.task.sys_id,notify.assigned_to);
}

 

Notification Email Script:

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

        var rrecord = new GlideRecord('sc_task'); //create new query object on the catalog task table.
		rrecord.addQuery('request_item', event.parm1); // query the catalog task table for all tasks associated with the request item's sys_id sent over through parm 1.
		rrecord.addQuery('active', true); // query the catalog tasks table only for active tasks.
		rrecord.query(); // run query
					
		while(rrecord.next()){ 
		var sctaskLink = rrecord.getLink();	//get the link to the sc_task record.
			
		template.print('Catalog task: ' + '<a href="' + sctaskLink + '">' +rrecord.number +'</a>' + '<br>' );	//print the link
		template.print('Short description: ' + rrecord.short_description + '<br>');	//print the short description
		template.print('Assignment group: ' + rrecord.assignment_group.getDisplayValue() + '<br>'); //print the assignment group value
		template.print('Assigned to: ' + rrecord.assigned_to.getDisplayValue() + '<br><br><br>'); //print the assigned to value
		
		
		}
	

})(current, template, email, email_action, event);
1 ACCEPTED SOLUTION

Thank you for your help. I've figured this out. I needed to use ArrayUtil with the ".unique" method to parse out the unique values. Then I sent them through Event parm 2 as a comma separated value. On my email notification I have the "recipients in Event parm 2" box checked. So even though there are multiple records assigned to each user, it only generates one event and sends each user through parameter 2, one time. I query and print the all the associated records in my notification script. 

 

var array1 = [];

var notify = new GlideRecord('sc_task');
notify.addQuery('request_item', current.task.sys_id);
notify.addActiveQuery();
notify.query();

while(notify.next()){
 array1.push(notify.assigned_to + ''); 
}

var arrayUtil = new ArrayUtil();
gs.eventQueue('sla.warning.50',current,current.task.sys_id,arrayUtil.unique(array1));

View solution in original post

5 REPLIES 5

SanjivMeher
Kilo Patron
Kilo Patron

You can try

Group them by assigned to

var notify = new GlideAggregate('sc_task');
notify.addQuery('request_item', current.task.sys_id);
notify.addQuery('active', true);
notify.groupBy('assigned_to');
notify.query();

while(notify.next()){
  gs.eventQueue('sla.warning.50',current,current.task.sys_id,notify.assigned_to);
}

 

Then query based on assigned to as well in m ail script

 

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

        var rrecord = new GlideRecord('sc_task'); //create new query object on the catalog task table.
		rrecord.addQuery('request_item', event.parm1); // query the catalog task table for all tasks associated with the request item's sys_id sent over through parm 1.
                rrecord.addQuery('assigned_to', event.parm1);  //query based on assigned to
		rrecord.addQuery('active', true); // query the catalog tasks table only for active tasks.
		rrecord.query(); // run query
					
		while(rrecord.next()){ 
		var sctaskLink = rrecord.getLink();	//get the link to the sc_task record.
			
		template.print('Catalog task: ' + '<a href="' + sctaskLink + '">' +rrecord.number +'</a>' + '<br>' );	//print the link
		template.print('Short description: ' + rrecord.short_description + '<br>');	//print the short description
		template.print('Assignment group: ' + rrecord.assignment_group.getDisplayValue() + '<br>'); //print the assignment group value
		template.print('Assigned to: ' + rrecord.assigned_to.getDisplayValue() + '<br><br><br>'); //print the assigned to value
		
		
		}
	

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

Please mark this response as correct or helpful if it assisted you with your question.

Thank you! I will try this and see if it works

Unfortunately this didn't work. My issue is that I only want it to trigger my event once for each user it finds, because the user is being sent as an event parameter. In this particular test I assigned 3 tasks for each user and it triggered my event for all six records it found. My desired outcome would have been for it to only trigger the event twice in this case because there was only two users with 3 records each. I've included a screenshot of the event log. Parameter 2 is the two users. Is there a way to only have it trigger once for each user that it's grouped by?

 

find_real_file.png

One correction to your script. Since I am using group By, it will only return you unique assigned to

 

var notify = new GlideAggregate('sc_task');
notify.addQuery('request_item', current.task.sys_id);
notify.addQuery('active', 'true');
notify.groupBy('assigned_to');
notify.query();

while(notify.next()){
  gs.eventQueue('sla.warning.50',current,current.task.sys_id,notify.assigned_to);
}

Please mark this response as correct or helpful if it assisted you with your question.