How to correctly combine two GlideRecord calls is one script

Victoria Kondra
Tera Contributor

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: 

// var value = new GlideRecord('sc_task');
// value.addQuery('sys_id', current.number);
// value.query();
// while (value.next()) {
    
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();
}
}

}

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 

1 ACCEPTED SOLUTION

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.

View solution in original post

21 REPLIES 21

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();
}

}

}

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();
}
}
}
}

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. 

find_real_file.png

find_real_file.png

find_real_file.png

David, 

 

I got the values in the Task, but the workflow is still an issue. 

 

If you have a chance, please let me know what is wrong there? 

 

Thanks

 

find_real_file.png

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.