Copy multi-row variables values to another RITM

dianemiro
Kilo Sage

Hi Everyone,

I am trying to create a new request from another request. Script below is working and it creates the new request but the problem is it is not copying the multi-row variables from the current RITM to the newly created RITM. What am I doing wrong? Thank you so much in advance.

createRequest();

function createRequest() {
	var cart = new Cart();
	var item = cart.addItem('4ff27719db867f0002b63ebd7c9619c9');

	var idnum = current.sys_id;
	
	var gr = new GlideRecord('sc_req_item');
	if(gr.get(idnum)){
		var variables = gr.variables.getElements();
		var rowcount = gr.variables.gaf_add_server.getRowCount();
		
		for(var rc = 0; rc < rowcount; rc++){
			var row = gr.variables.gaf_add_server.getRow(rc);
			var role = row.server_role;
			var name = row.server_name;
			var location = row.server_location;
			var ip_address = row.primary_ip_address;
			var cep = row.cep;
			var comments = row.comments;
			
			//Set Variables in your Cart Item
			cart.setVariable(item, 'company',current.variables.company);
			cart.setVariable(item, 'requested_for',current.variables.requested_for);
			cart.setVariable(item, 'server_role', role);
			cart.setVariable(item, 'server_name', name);
			cart.setVariable(item, 'server_location', location);
			cart.setVariable(item, 'primary_ip_address', ip_address);
			cart.setVariable(item, 'cep', cep);
			cart.setVariable(item, 'comments', comments);
		}
		var rc = cart.placeOrder();
		gs.addInfoMessage('Request Item Created: ' + rc.number);
	}
	
}
1 ACCEPTED SOLUTION

dianemiro
Kilo Sage

I was able to get this running by using the following code:

createRequest();

function createRequest() {
	//Add item to existing request
	var addBillingItem = '4ff27719db867f0002b63ebd7c9619c9';
	var reqHelper = new GlideappCalculationHelper();
	reqHelper.addItemToExistingRequest(current.request, addBillingItem, 1); // 1 is the qty
	reqHelper.rebalanceRequest(current.request);
	
	//find the item and update itsvariables
	var grReqItem = new GlideRecord('sc_req_item');
	grReqItem.addQuery('request', current.request);
	grReqItem.addQuery('cat_item',addBillingItem);
	grReqItem.addQuery('parent','');
	grReqItem.query();
	
	if(grReqItem.next()){
		//set the simple variables on the new RITM.
		grReqItem.variables.company = current.variables.company;
		grReqItem.variables.requested_for = current.variables.requested_for;
		grReqItem.update();
		
		//get the variables from multi-row variable set
		var ritmSysID = current.sys_id;
		if(current.get(ritmSysID)){
			//var variables = current.variables.getElements();
			var newRITM = grReqItem.variables.gaf_add_server;
			var rowcount = current.variables.gaf_add_server.getRowCount();
			
			for(var rc = 0; rc < rowcount; rc++){
				var row = current.variables.gaf_add_server.getRow(rc);
				var role = row.server_role;
				var name = row.server_name;
				var location = row.server_location;
				var ip_address = row.primary_ip_address;
				var cep = row.cep;
				var comments = row.comments;
				
				//set the values of multi-row set on the new RITM.
				var newRow = newRITM.addRow();
				newRow.server_role = role;
				newRow.server_name = name;
				newRow.server_location = location;
				newRow.primary_ip_address = ip_address;
				newRow.cep = cep;
				newRow.comments = comments;
			}
			grReqItem.update();
		}
	}
}

View solution in original post

2 REPLIES 2

dianemiro
Kilo Sage

I was able to get this running by using the following code:

createRequest();

function createRequest() {
	//Add item to existing request
	var addBillingItem = '4ff27719db867f0002b63ebd7c9619c9';
	var reqHelper = new GlideappCalculationHelper();
	reqHelper.addItemToExistingRequest(current.request, addBillingItem, 1); // 1 is the qty
	reqHelper.rebalanceRequest(current.request);
	
	//find the item and update itsvariables
	var grReqItem = new GlideRecord('sc_req_item');
	grReqItem.addQuery('request', current.request);
	grReqItem.addQuery('cat_item',addBillingItem);
	grReqItem.addQuery('parent','');
	grReqItem.query();
	
	if(grReqItem.next()){
		//set the simple variables on the new RITM.
		grReqItem.variables.company = current.variables.company;
		grReqItem.variables.requested_for = current.variables.requested_for;
		grReqItem.update();
		
		//get the variables from multi-row variable set
		var ritmSysID = current.sys_id;
		if(current.get(ritmSysID)){
			//var variables = current.variables.getElements();
			var newRITM = grReqItem.variables.gaf_add_server;
			var rowcount = current.variables.gaf_add_server.getRowCount();
			
			for(var rc = 0; rc < rowcount; rc++){
				var row = current.variables.gaf_add_server.getRow(rc);
				var role = row.server_role;
				var name = row.server_name;
				var location = row.server_location;
				var ip_address = row.primary_ip_address;
				var cep = row.cep;
				var comments = row.comments;
				
				//set the values of multi-row set on the new RITM.
				var newRow = newRITM.addRow();
				newRow.server_role = role;
				newRow.server_name = name;
				newRow.server_location = location;
				newRow.primary_ip_address = ip_address;
				newRow.cep = cep;
				newRow.comments = comments;
			}
			grReqItem.update();
		}
	}
}

@dianemiro where can i use this script like client script or script include ?