Workflow - Get the Values from the last Catalog Task closed

alexcolbert
Kilo Guru

Hi Community,

 

I have included a "Wait for Condition" step in my workflow for Requested Items.  Basically it waits until all catalog tasks have been closed and then it closes the RITM ticket.  This includes Catalog Tasks created in the workflow and manually created Catalog Tasks.

 

This works great.

 

I am attempting to modify this script so that I can capture data from the last Catalog Task to be closed, and then use this data to trigger an event.

 

The particular field I am trying to capture is a custom field called country on the sys_user_group table.  The script I currently have is included below:

//Query for all tasks to see if they are inactive
var rec = new GlideRecord('sc_task');
 rec.addQuery('request_item', current.sys_id);
 rec.addQuery('assignment_group', rec.assignment_group);
 rec.addQuery('active', true);
 rec.query();
var group = rec.assignment_group;
if(rec.hasNext()){
 answer = false;
}
var con = new GlideRecord("sys_user_group");
		con.addQuery('u_country', group.u_country);
		con.query();
if(!rec.hasNext()){
 answer = true;
 gs.eventQueue("sc_req_item.closed.ticket", current, group.u_country);
 }

The event is firing successfully, but it is not pulling in the country field that I need.

I'd appreciate it if anyone has any ideas on this.

 

Thanks!

Alex

 

1 ACCEPTED SOLUTION

If you're looking to get the last task closed you would need a different query than checking to see if there are any opened tasks. Something like this could work:

var rec = new GlideRecord('sc_task');
rec.addQuery('request_item', current.sys_id);
rec.addQuery('active', false);
rec.orderByDesc('sys_updated_on');
rec.query();
if (rec.next()) {
    gs.eventQueue("sc_req_item.closed.ticket", current, rec.assignment_group.u_country + '');
}

View solution in original post

4 REPLIES 4

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Alex,

Could you describe exactly what the code is supposed to be doing? There are a couple of lines that are problematic that jump out at me:

rec.addQuery('assignment_group', rec.assignment_group);
rec.assignment_group wouldn't have a value yet because you're still doing the gliderecord lookup

var group = rec.assignment_group;
rec.assignment_group wouldn't exist as you'd need to iterate into the record first.

What assignment group are you wanting to use there?

Hi Brad,

 

I'm trying to grab the value of the assignment group from the last Catalog Task to be closed i.e. the Catalog Task that will close the RITM.

 

I then want to take the value of a custom field from that assignment group and insert it into an event.

 

Do you think that would be possible?

If you're looking to get the last task closed you would need a different query than checking to see if there are any opened tasks. Something like this could work:

var rec = new GlideRecord('sc_task');
rec.addQuery('request_item', current.sys_id);
rec.addQuery('active', false);
rec.orderByDesc('sys_updated_on');
rec.query();
if (rec.next()) {
    gs.eventQueue("sc_req_item.closed.ticket", current, rec.assignment_group.u_country + '');
}

Thanks Brad, that worked perfectly!