Creating Multiple RITMs from one RITM in Workflow - Trouble setting variables

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2019 08:53 PM
Working through an issue with a workflow where I have 1 RITM that has a list of access level requests in a list collector. I've assigned the list collector to a scratchpad array and I want to parse through that array to create a new RITM for each of the items in the array. Here's my runscript:
for (var i = 1; i < workflow.scratchpad.access_list.length; i++) {
gs.sleep(1000);
gs.log('Assignment Access Level ' +workflow.scratchpad.access_list[i].toString(),workflow.scratchpad.wf_name);
var ritm = new GlideRecord('sc_req_item');
ritm.newRecord();
ritm.request = current.request;
ritm.cat_item = current.cat_item;
ritm.due_date=current.due_date;
ritm.variables.role_name=current.variables.role_name;
ritm.variables.access_level = workflow.scratchpad.access_list[i];
//ritm.variables.access_level="b0d67332e4c9e40006d80e38f69704df";
ritm.setWorkflow(true);
ritm.insert();
gs.sleep(1000);
}
When the script runs, I get a log entry that reads:
Assignment Access Level b0d67332e4c9e40006d80e38f69704df
But when the new RITM is created, the access_level variable is blank. I was expecting the line:
ritm.variables.access_level = workflow.scratchpad.access_list[i].toString();
to properly set the access_level variable (a list collector) in the new RITM, but it is not.
Likewise, the line: ritm.variables.role_name = current.variables.role_name
is not properly setting the new RITM's role_name variable.
What am I missing here?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2019 11:37 PM
Thank you for jumping in Shariq.I'll work some more with the code you suggested. I think you have me on the right track. It's 3am here, though, so I'm in need of a break. I'll get back to this first thing in the morning
Here is my latest version of the code:
var scr=workflow.scratchpad.access_list;//for safer purpose
for (var i = 1; i < scr.length; i++) {
var ritm = new GlideRecord('sc_req_item');
ritm.newRecord();
ritm.request = current.request;
ritm.cat_item = current.cat_item;
ritm.due_date=current.due_date;
var rtm_sys=ritm.insert();
ritm.setWorkflow(false); //do you still need this? instead keep it false to stop br executions,resulting in faster performance
var itm=new GlideRecord('sc_item_option');
itm.initialize();
itm.item_option_new='6d6da3dc1baaeb003d3186e8cd4bcb04'; //sys_id of access_level variable.
itm.value=scr[i].toString();
var gr_mtom= itm.insert();
var varsOpt= new GlideRecord('sc_item_option_mtom');
varsOpt.initialize();
varsOpt.request_item=rtm_sys;
varsOpt.sc_item_option=gr_mtom;
varsOpt.insert();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2019 11:59 PM
before checking the below codes, i am sure you are trying only after publishing the workflow. Sometimes it doesnt work if not published.
two suggestions to my own code then(try them one by one first. finally applying both together):
1.remove ritm.setWorflow(false); line
2.maybe you can re-update the new ritms. I know this is a wrong approach, but doing just for the sake of it.
var scr=workflow.scratchpad.access_list;//for safer purpose
gs.log('print acces list '+scr);
for (var i = 1; i < scr.length; i++) {
var ritm = new GlideRecord('sc_req_item');
ritm.newRecord();
ritm.request = current.request;
ritm.cat_item = current.cat_item;
ritm.due_date=current.due_date;
var rtm_sys=ritm.insert();
//ritm.setWorkflow(false); //try commenting it first
gs.log('print access list ,ritm inserted '+scr[i]+' ' +ritm.number);
var itm=new GlideRecord('sc_item_option');
itm.initialize();
itm.item_option_new='0a9c63dc1baaeb003d3186e8cd4bcbfb'; //sys_id of access_level variable.
itm.value=scr[i].toString();
var gr_mtom= itm.insert();
gs.log('print access list ,option value inserted '+gr_mtom+' ' +ritm.number);
var varsOpt= new GlideRecord('sc_item_option_mtom');
varsOpt.initialize();
varsOpt.request_item=rtm_sys.toString());
varsOpt.sc_item_option=gr_mtom.toString());
var mtom= varsOpt.insert();
gs.log('print access list ,mtom inserted '+mtom+' ' +ritm.number);
var rtm_upd= new GlideRecord('sc_req_item');//sorry for the reiteration
rtm_upd.addQuery('sys_id',rtm_sys.toString());
rtm_upd.query();
if(rtm_upd.next()){
rtm_upd.variables['access_level']= scr[i].toString();//check the name of the variable
rtm_upd.update() //i know this is a wrong approach
gs.log('print access list ,ritm updated '+rtm_upd.variables['access_level']+' ' +ritm.number);
}
}
Btw can you please check if the records are being created in mtom table? along with the right logs?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2019 02:44 AM
Hi,
Can you kindly provide an update to the above item?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2022 01:20 AM
i used this script on my requirement. It is creating multiple RITM successfully but workflow is not attached to those RITM and One RITM contain all variables from list collector and workflow also attaches to it but others not. Do you know where i go wrong?