
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2020 02:54 AM
Dear All,
I am very new to scripting, please suggest how to correctly write a script where I need to use GlideRecord call twice.
The script is here:
// value.addQuery('sys_id', current.number);
// value.query();
// while (value.next()) {
var catVar = current.variables.select_all_tw_software_needed.toString();
var res = finalVal.split(',');
var len = res.length;
{
for( var i=0; i<len;i++)
{
var table = new GlideRecord('u_asr_apps_to_users');
table.initialize();
table.u_nameid_servicenow_applicaion = res[i];
//table.u_parent_sc_task = value;
table.insert();
}
}
}
The logic is to get current SCTASK number based on sys_id of the record and inset it in a new custom table 'u_asr_apps_to_users'. The 1st part of the script isn't working.
I assume I am missing some important element I am trying to query sc_task table and get the task number,but I am not sure that I am doing it correctly. Please help!
Thanks,
Victoria
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2020 05:13 AM
OK so you could try moving the script in your run script activity to the script section on the catalog task activity, not 100% sure if that would work though, if it does you wouldn't need the glide record you could just reference current.number as the parent task record.
If that doesn't work you'll need to add a wait for condition in your workflow after the run script otherwise it will skip right through to the end. Wait for conditions are only checked when the current record is updated so you'll need something to change on the parent request item record once the task is done in order to satisfy that condition. I think OOB config marks the request item as closed complete when the all tasks are completed so you might be able to just check for when state changes to closed complete. If not you'll have to find something else to change so that the workflow checks the condition.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2020 03:27 AM
Thanks David,
I understood where to add the 1st line of code.
My question is more for the 2nd code on Run Script.
By the rest of the code you mean this?
var catVar = current.variables.select_all_tw_software_needed.toString();
var catVar2 = current.variables.select_all_3rd_party_software_needed.toString();
var finalVal= catVar+','+catVar2;
var res = finalVal.split(',');
var len = res.length;
if(len>0)
{
for( var i=0; i<len;i++)
{
var table = new GlideRecord('u_asr_apps_to_users');
table.initialize();
table.u_nameid_servicenow_applicaion = res[i];
table.u_parent_sc_task = value;
table.insert();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2020 03:33 AM
Yes, the only change you need to make is to u_parent_sc_task to be the display value of the task, value on it's own is the glide record.
var value = new GlideRecord('sc_task');
if(value.get(workflow.scratchpad.task_id)){
var catVar = current.variables.select_all_tw_software_needed.toString();
var catVar2 = current.variables.select_all_3rd_party_software_needed.toString();
var finalVal= catVar+','+catVar2;
var res = finalVal.split(',');
if(res.length>0)
{
for( var i=0; i<res.length; i++)
{
var table = new GlideRecord('u_asr_apps_to_users');
table.initialize();
table.u_nameid_servicenow_applicaion = res[i];
table.u_parent_sc_task = value.getDisplayValue();
table.insert();
}
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2020 04:05 AM
Many thanks, David!
It's all working now I got the number where I need it!
But I have two small issues:
1. We all need to do it for an ability to see these records into the related list of the given task, but even though the task number has been correctly populated it's not getting displayed under related list of the task. Why? What am I missing?
2. I have put the Run Script after Task in my workflow since I need this number and unchecked "Wait for completion" check box, since I need records in the table before I close the task. This is the main point, the ITIL users will work on related list part of the task on the variable section. But what happens the system closes the task right after it's creation when this box is unchecked.
Below I am attaching screens.
If you have any idea what I am doing wrong here, please let me know.
Many thanks.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2020 04:41 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2020 05:13 AM
OK so you could try moving the script in your run script activity to the script section on the catalog task activity, not 100% sure if that would work though, if it does you wouldn't need the glide record you could just reference current.number as the parent task record.
If that doesn't work you'll need to add a wait for condition in your workflow after the run script otherwise it will skip right through to the end. Wait for conditions are only checked when the current record is updated so you'll need something to change on the parent request item record once the task is done in order to satisfy that condition. I think OOB config marks the request item as closed complete when the all tasks are completed so you might be able to just check for when state changes to closed complete. If not you'll have to find something else to change so that the workflow checks the condition.