Calling a catalog item / order guide multiple times from workflow depending on # of that item requested.

Stephen31
Kilo Expert

Hi All,

I have a tricky one I'm trying to wrap my head around.  We have a catalog item that's currently calling an order guide as part of its workflow.  This catalog item has a widget that people are using to input the data, the data is being passed from widget to hidden variables.  The catalog item passes those variables on to the order guide that cascades it on down to the other items and all of this seems to be working well with the exception of one.

There's one catalog item that people can order multiple times.  The widget itself can create new rows for people to order the same item multiple times but each time has different information.  All of this information is concatenated and dumped into a single multi-line variable that contains the row#, and the 3 choices selected for each row.

Somehow, either through the order guide or separate from it, I need to look at the number of rows in this field, and call the appropriate catalog item that many times with the information from that particular row plus the other form variables being passed to it.

Anyone have any thoughts on the best way to handle this?

1 ACCEPTED SOLUTION

Stephen31
Kilo Expert

Figured this one out.  Posting the code in case anyone else runs into a similar issue.

(function(){
	
	//call API and set the variable which the RITMS will be created from.
	var reqHelper = new GlideappCalculationHelper();
	var yourVar = current.variables.variable_with_multi_values.toString();

	//Split the contents of the variable into seperate pieces at each |
	var yourVarArray = yourVar.split('|');

	//For each piece, run through a loop and do the following.
	var count = yourVarArray.length;	
	for (var i=0; i < yourVarArray.length; i++) {

		// add each file selected as a separate item to current request
		reqHelper.addItemToExistingRequest(current.request, 'sys_ID_of_cat_item_you_want_to_call', 1); // 1 is the qty
		reqHelper.rebalanceRequest(current.request);

		//Populate the values of the variables on the new RITMs with the values from the parent
		var grReqItem = new GlideRecord('sc_req_item');
		grReqItem.addQuery('request', current.request);
		grReqItem.addQuery('cat_item','sys_ID_of_cat_item_you_want_to_call');
		grReqItem.addQuery('parent','');
		grReqItem.query();
		if(grReqItem.next()) {
		grReqItem.parent = current.sys_id;		
		grReqItem.variables.application = current.variables.application;
		grReqItem.variables.costcenter = current.variables.costcenter;
		grReqItem.variables.requester = current.variables.requester;	
		//Set the value of variable_with_multi_values in new item to be the one piece it was created from in the loop.
		grReqItem.variables.variable_with_multi_values = yourVarArray[i];
		workflow.scratchpad.rowData = grReqItem.variables.variable_with_multi_values;
			
		grReqItem.update();
		}
   }
		
})();

View solution in original post

4 REPLIES 4

Stephen31
Kilo Expert

This is what I have tried so far.  Its creating only one new RITM on the original request however and the variables are not copying over.

var orig_req = current.request;
var orig_ritm = current.number;
var orig_sys_id = current.sys_id;
var orig_submitter = current.opened_by;

var micro_cat_item = '71d1df2bdb13e3040726f260399619a5';

var microservices = current.variables.v_microservices;

var ind_mic = microservices.toString().split('|');

for(var i = 0; i < ind_mic.length; i++){ 
	var mic = ind_mic[i];

	var helper = new GlideappCalculationHelper();
	helper.addItemToExistingRequest(orig_req, micro_cat_item, "1");
	helper.rebalanceRequest(orig_req);
}

                var micOrg = new GlideRecord('sc_item_option_mtom');
		micOrg.addQuery('request_item.sys_id', nid);
		micOrg.query();		
		micOrg.deleteMultiple();

		var micVar = new GlideRecord('sc_item_option_mtom');
		micVar.addQuery('request_item.sys_id', orig_sys_id);
		micVar.query();

		while (micVar.next()) {
			var varCopy = new GlideRecord('sc_item_option_mtom');
			varCopy.newRecord();
			varCopy.sc_item_option = micVar.sc_item_option;
			varCopy.request_item = nid;
			varCopy.insert();
		}

Stephen31
Kilo Expert

 This is where I'm at now.  Its creating the right number of RITMs in the proper request but I've tried several ways to pass the variables to the ritms that get created and I'm having no luck. 

(function(createRitms){
	
	var reqHelper = new GlideappCalculationHelper();
	var micro = current.variables.v_microservices.toString();
	var microArray = micro.split('|');
	
	var count = microArray.length;
	
	for (var i=0; i < microArray.length; i++) {
		// add each file selected as a separate item to current request
		reqHelper.addItemToExistingRequest(current.request, '71d1df2bdb13e3040726f260399619a5', 1); // 1 is the qty
	}
	reqHelper.rebalanceRequest(current.request);
		
})();

// grab common variables from original 
(function(getVariables){
var application = current.variables.application;
var costCenter = current.variables.costcenter;
var requester = current.variables.requester;
var workspace = current.variables.workspace;
var micro = current.variables.v_microservices.toString();
var orig_sys_id = current.sys_id;

	
	var microArray = micro.split('|');
	
// Loop through variable array
	if (microArray){
		for (var i=0; i < microArray.length; i++) {
			// update/add values to variables on item
			var grReqItem = new GlideRecord('sc_req_item');
			grReqItem.addQuery('request',current.request);
			grReqItem.setLimit(1);
			grReqItem.query();
			while(grReqItem.next()) {
				grReqItem.variables.application = application;
				grReqItem.variables.costCenter = costCenter;
				grReqItem.variables.requester = requester;
				grReqItem.update();
			
			}
		  }
		}
	})();

Stephen31
Kilo Expert

Any help would definitely be appreciated.  Just can't figure out how to get the variables to pass to the new RITMs getting created.  Also ideally I need only the piece of the microServices string that relates to that particular RITM to be included in that RITMs "v_microservices" variable.

Supposed to do a demo tomorrow and this piece is just kicking my arse.

Stephen31
Kilo Expert

Figured this one out.  Posting the code in case anyone else runs into a similar issue.

(function(){
	
	//call API and set the variable which the RITMS will be created from.
	var reqHelper = new GlideappCalculationHelper();
	var yourVar = current.variables.variable_with_multi_values.toString();

	//Split the contents of the variable into seperate pieces at each |
	var yourVarArray = yourVar.split('|');

	//For each piece, run through a loop and do the following.
	var count = yourVarArray.length;	
	for (var i=0; i < yourVarArray.length; i++) {

		// add each file selected as a separate item to current request
		reqHelper.addItemToExistingRequest(current.request, 'sys_ID_of_cat_item_you_want_to_call', 1); // 1 is the qty
		reqHelper.rebalanceRequest(current.request);

		//Populate the values of the variables on the new RITMs with the values from the parent
		var grReqItem = new GlideRecord('sc_req_item');
		grReqItem.addQuery('request', current.request);
		grReqItem.addQuery('cat_item','sys_ID_of_cat_item_you_want_to_call');
		grReqItem.addQuery('parent','');
		grReqItem.query();
		if(grReqItem.next()) {
		grReqItem.parent = current.sys_id;		
		grReqItem.variables.application = current.variables.application;
		grReqItem.variables.costcenter = current.variables.costcenter;
		grReqItem.variables.requester = current.variables.requester;	
		//Set the value of variable_with_multi_values in new item to be the one piece it was created from in the loop.
		grReqItem.variables.variable_with_multi_values = yourVarArray[i];
		workflow.scratchpad.rowData = grReqItem.variables.variable_with_multi_values;
			
		grReqItem.update();
		}
   }
		
})();