Create Multiple RITM from Single catalog request

mahesh105
Tera Contributor

We have a list collector field (location_requiring_access_to.name) on form. if a user selects multiple locations in _different_ states, there should be separate RITMs that are generated for the various state compliance managers to approve.  

I have written script in runscript activity. below is the script

Run script:

workflow.scratchpad.loc = current.variables.location_requiring_access_to.name.toString().split(',');
//var catItem = '7475493a1bc0511013e1766e034bcbfa';
var loc = workflow.scratchpad.loc;

// var loc = current.variables.location_requiring_access_to.toString().split(',');
for (i = 0; i < loc.length; i++) {
gs.log('push');
createRequest(loc[i].toString());
}

function createRequest(locationRequiringAccess) {
gs.log('push1');
//var item = cart.addItem("ec8c89be1bc0511013e1766e034bcbba");
var item = cart.addItem(); //var item = cart.addItem(catItem);

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
cart.setVariable(item, 'Location Requiring Access to:', current.variables.location_requiring_access_to);

var rc = cart.placeOrder();
gs.log('push2' + rc.number);
var gr = new GlideRecord('sc_req_item');
gr.addQuery('request', rc);
gr.query();
if (gr.next()) {
gs.log('push3');
gr.request.location_requiring_access_to = locationRequiringAccess.toString();
gr.update();
}
}

mahesh105_0-1667979060766.png

 

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

I'll assume this isn't working, either at all, or not how you expect.  I'll share a script I use that may help.  In this scenario the List Collector variable is on the cmdb_ci table, so for each CI selected an RITM is created within the same REQ, and the same simple workflow is executed on each RITM.  This also converts the original RITTM into only processing the first CI selected, and also works if only one CI is selected.  This means you don't need an approval activity and path for each possible location in your scenario, unless there's something different that happens.

var cilist=[];
var ritmsysid = '';

//get sysids of variables that will be populated on new RITMs
var v_db_cat = '9f29df1bdbe48f004a29df6b5e9619c3';
var v_integration_status = 'bbfcbc9cdb298b00262950a45e961958';

//get multiple values, split into an array
var items = current.variables.v_db_cat.toString(); 
items = items.split(',');

//if only one item was selected, set the values per usual
if(items.length == 1){
	var ci = new GlideRecord ('cmdb_ci');
	ci.addQuery('sys_id', items[0]);
	ci.query();
	if(ci.next()){
		current.short_description = current.cat_item.name + ' - ' + ci.name;
	}
}
//or else set the values on the initial, existing RITM, and change the selected item to the first one
else{
	current.variables.v_db_cat = items[0];
	var ci = new GlideRecord ('cmdb_ci');
	ci.addQuery('sys_id', items[0]);
	ci.query();
	if(ci.next()){
		current.short_description = current.cat_item.name + ' - ' + ci.name;
	}

	//create a new RITM under the same REQ for all of the other selected values
	for (var i = 1; i < items.length; i++) {
		var ritm = new GlideRecord('sc_req_item');
		ritm.initialize();
		ritm.request = current.request;
		ritm.cat_item = current.cat_item;
		ritm.opened_by = current.opened_by;
		var ci2 = new GlideRecord ('cmdb_ci');
		ci2.addQuery('sys_id', items[i]);
		ci2.query();
		if(ci2.next()){
			ritm.short_description = current.cat_item.name + ' - ' + ci2.name;
		}
		var sysID = ritm.insert();  

		//Add variables to the new RITMs  
		addVariable(sysID, v_integration_status, current.variables.v_integration_status,1);  
		addVariable(sysID, v_db_cat, items[i],2);  
	}  
	//Start the workflow on all the new RITMs  
	startWf(current.request);  
}  

function addVariable(ritm,varID,value,order) {  
	var variable = new GlideRecord('sc_item_option');  
	variable.initialize();  
	variable.item_option_new = varID;  
	variable.value = value;  
	variable.order = order;  
	var sysID = variable.insert();  

	var itemm2m = new GlideRecord('sc_item_option_mtom');  
	itemm2m.initialize();  
	itemm2m.request_item = ritm;  
	itemm2m.sc_item_option = sysID;  
	itemm2m.insert();  
}  

function startWf(request) {  
	var ritm = new GlideRecord('sc_req_item');  
	ritm.addQuery('request',request);  
	ritm.addQuery('sys_id','!=',current.sys_id);  
	ritm.addNullQuery('context');  
	ritm.query();  

	while (ritm.next()) {  
		ritm.setForceUpdate(true);  
		ritm.update();  
	}  
}  

Below script is working. It is creating multiple ritm in global but i want this ritm to be created in specific domain under same request.

Runscript:

Run script:

var cart = new Cart(cartId);
for (var i = 0; i < loc.length; i++) {
var item = cart.addItem(cat_name, 1);
cart.setVariable(item, "Location Requiring Access to:", i);
gs.log('boo3');
//createRequest(loc[i].toString());
gs.log('boo2');
}

//function createRequest(locationRequiringAccess) {

 

var rc = cart.placeOrder();
gs.log('boo5' + rc.number);
// var gr = new GlideRecord('sc_req_item');
//gr.addQuery('request', rc);
//gr.query();
//if (gr.next()) {
// gs.log('boo4');
// gr.request.location_requiring_access_to = locationRequiringAccess.toString(); //locationRequiringAccess.toString()
// gr.update();

//}

//}

Hi @Brad Bowman ,

 

Your script is quite useful and is working for the most part. However, just one issue I am seeing in my use case the script creates 1 default RITM before the for loop is run which has all the variables and information. My goal is to create each RITM with additional logic in the for loop and set the variables as per the updated logic. Please see a sample of my script below and advise if you spot anything off or if there's anything else I can try.

 

For example: 

 

var variable_name1 = var_sysID1,
      variable_name2 = var_sysID2,
      variable_name3 = var_sysID3,
      variable_name4 = var_sysID4;

var items = JSON.parse(current.variables.variable_name3);
// variable_name3 is a multiline text field formatted as follows:
// [{"name:"ABC", "subArr": ["item1", "item2"]}, {"name": "XYZ", "subArr": ["item3", "item4", "item5"]};
// RITM count should be equal to items.length
// If items.length =2  then the script below creates 3 RITMs. The first one with all the variable values as entered by the //requestor and the remaining 2 RITMs appear to have the correct variable assignment but some RITM fields are not //populated (u_requested_for & country);

for (var i = 0; i < items.length; i++) {
	var ritm = new GlideRecord('sc_req_item');
	ritm.initialize();
	ritm.request = current.request;
	ritm.cat_item = current.cat_item;
	ritm.opened_by = current.opened_by;
	ritm.u_requested_for = current.variables.variable_name1;
	ritm.u_country = current.variables.variable_name2;    

	var sysID = ritm.insert();
addVariable(sysID, item_requested_for, current.variable_name1, 10);  
	addVariable(sysID, country, current.variables.variable_name2, 10);
	addVariable(sysID, variable_name3, items[i].name, 100);  
	addVariable(sysID, variable_name4, items[i].subArr.join(), 125);
startWf(current.request)
}

 

 

Abhiii1
Tera Contributor

Hey Gill , 

were you able to get the solution to this ? 
when I ran the code given by brad, it did the same thing as you described.
in the second RITM , there was no workflow attached. and no variables aswell.

if you did find the solution could you please share?