- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2022 06:22 AM
Hello I have a workflow where I have a task and after it there is a script to close it automatically but I am not able to get its sys id to do this, I will leave my code below.
Field Advanced Task :
var taskid = task.setNewGuid();
workflow.scratchpad.taskid = taskid;
Run Script to closed Task:
var gr2 = new GlideRecord('sc_task');
gr2.addQuery('requested_item', taskid );
gr2.addQuery('active', true);
gr2.query();
if (gr2.next()) {
gr2.state = '3';
gr2.update();
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2022 06:41 AM
Hi,
I'd recommend (which I believe that's what you're showing that you've done) retrieving the task sys_id in the advanced script section of the task activity in the workflow like so:
workflow.scratchpad.taskSysID = task.setNewGuid();
Then, elsewhere, you can use it like so:
var gr2 = new GlideRecord('sc_task');
gr2.get(workflow.scratchpad.taskSysID);
gr2.setValue('state', 3);
gr2.update();
So I believe you were on the right track, you were just using the sys_id you got from the previous activity...as the request_item in your GlideRecord and that wasn't it.
Please mark reply as Helpful/Correct. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2022 06:41 AM
Hi,
I'd recommend (which I believe that's what you're showing that you've done) retrieving the task sys_id in the advanced script section of the task activity in the workflow like so:
workflow.scratchpad.taskSysID = task.setNewGuid();
Then, elsewhere, you can use it like so:
var gr2 = new GlideRecord('sc_task');
gr2.get(workflow.scratchpad.taskSysID);
gr2.setValue('state', 3);
gr2.update();
So I believe you were on the right track, you were just using the sys_id you got from the previous activity...as the request_item in your GlideRecord and that wasn't it.
Please mark reply as Helpful/Correct. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2022 06:42 AM
Is your workflow on RITM table and you have only one catalog task for it.
Then modify your run script to below in run script
var gr2 = new GlideRecord('sc_task');
gr2.addQuery('request_item', current.sys_id );
gr2.query();
if (gr2.next()) {
gr2.state = '3';
gr2.update();
}
Mark as correct or helpful if it does.
Regards,
Sumanth

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2022 07:02 AM
So on this line
gr2.addQuery('requested_item', taskid );
you are using the requested_item field, but if I understand the code taskid is the catalog task sys_id so you should use
gr2.addQuery('sys_id', taskid );
or do
if(gr2.get(taskid)){
//close task code
}
also I can only assume that you did something like
var taskid = workflow.scratchpad.taskid;
at some point since you clearly did not post all of the code.