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

Brian Bouchard
Mega Sage

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?

23 REPLIES 23

Variable editor is added to the form view for sc_req_item

Hi 

Did you check sc_item_option_mtom table to actually see if the variables are created? 

 

Try System property glide.sc.use_sc_form_v2 set to false

There are 2 RITMS attached to the REQ:

find_real_file.png

Try code provided by shariq. Variables will not get copied if create RITM using code.

Sharique Azim
Kilo Sage

TLDR; read the what you can rather do section.

 

What are you missing? the interpretation of the usage of variables.

 

Where are you wrong? expecting the  created from script ritms to  insert and set variables ,even before the particular variables of the catalog item are accessed and attached with the RITM.

 

ritm.cat_item = current.cat_item; will  set the  field of the new request item with the sys id of the catalog item, but doesn't knows yet which variables are contained in it.

 

And if you understand the process of variable creation in requested item form (NOTcatalog item form), then your approach is wrong.

 

What you can rather do?

This is rather a long but correct approach.

First you should know create a mtom option for the requested item and then you can save the value of the list collector.

 

please take help from the script below. BTW usage of sleep function is literally going to degrade your  instance performance. Thus,edited.

var scr=workflow.scratchpad.access_list;//for safer purpose
for (var i = 1; i < scr.length; i++) {

//	gs.sleep(1000); use timer insteads
	gs.log('Assignment Access Level ' +scr[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";
var rtm_sys=ritm.insert();
//	gs.sleep(1000); refrain fro using it.
	ritm.setWorkflow(true); //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='sys_id_of_the list_collector_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();
	
	
}