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

kente
Tera Guru

I am not quiet sure why its not working. But since its just the same two field every time i would use a function to create the new records.

Also do not make a split like that. It could potentially hold a "," in the name.
Instead push the values your going through into an array and go through the array

createRecord(current.variables.select_all_tw_software_needed.toString());
createRecord(current.variables.select_all_3rd_party_software_needed.toString());


function createRecord(value){
 var table = new GlideRecord('u_asr_apps_to_users');
 table.initialize();
 table.u_nameid_servicenow_applicaion = value;
 table.u_parent_sc_task = current.sys_id;
 table.insert()
}

Utpal Dutta1
Mega Guru

Hey Victoria,

 

Here is a sample code that will help you achieve your solution. I used this code to send CAB Approvals on Change Form through UI Action.

 

Script:

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group','b85d44954a3623120004689b2d5dd60a');
gr.query();

while (gr.next()) {
var gr1 = new GlideRecord('sysapproval_approver');
gr1.initialize();
gr1.setValue('sysapproval',current.sys_id);
gr1.setValue('approver',gr.user);
gr1.setValue('state','requested');
gr1.insert();
}

 

If it helps then please mark my comment Helpful and Correct.

 

Thanks and Regards;

Utpal Dutta