
- 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:03 AM
Where are you writing this script? You initial glide record appears to be querying for a sys_id but passing in the record number, if you're on the request item form and looking to get all child requests then the query will look like this:
var value = new GlideRecord('sc_task');
value.addQuery('request_item', current.getUniqueValue());
value.query();
gs.log('this query as returned ' + value.getRowCount() + ' records');
while (value.next()) {
//rest of your code
}
The gs.log will log to the script log statements module so you can check any debugging lines there.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2020 03:07 AM
David,
Thanks for the prompt answer.
I am writing it on Run Script in Workflow.
I need only SCTASK number and few variables from the task to copy to custom table. I don't need all records.
Knowing this, how would you modify the script?
Thanks,
Victoria
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2020 03:14 AM
OK, so to get the sys_id of a task you have created in the workflow you can print the value to the workflow scratchpad when you create the task, add a line like below to the script box in the script section of the create catalog task activity (tick the advanced box to reveal it!)
workflow.scratchpad.task_id = task.setNewGuid();
You can then use that in your run script activity like so:
var value = new GlideRecord('sc_task');
if(value.get(workflow.scratchpad.task_id)){
//rest of your code
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2020 03:17 AM