Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Is it possible to create multiple RITM from a list collector?

JLeong
Mega Sage

Hi All,

'Hope everyone is safe and healthy.

Is it possible to create multiple requested items (sc_req_item) based on a list collector? For example, if I have 5 items listed from the list collector, it will also create 6 RITM. Like order guide but from a regular request form.

If so, please provide the steps to do it.

Thank you in advance.

Regards,

Jocelyn

 

1 ACCEPTED SOLUTION

Hi Saurab,

Thanks for sharing your script. It gave me ideas and helped me with what I need to accomplish.

I was able to create a catalog item for each list collector from a record producer. Attached is the script.

I also used Flow Designer to automate the fulfillment.

 

//*** cancel creation of global record ***//
current.setAbortAction(true);

//*** Create Service Catalog Request (SR) ***//
var newReq = new GlideRecord('sc_request');
newReq.initialize();
newReq.opened_by = gs.getUserID();
newReq.due_date = gs.endOfThisWeek();
newReq.short_description = 'ServiceNow Group Access Request';
if (producer.sna_request_for_other == 'no') {
newReq.requested_for = gs.getUserID();
} else {
newReq.requested_for = producer.sna_request_is_for;
}
var newID = newReq.insert();


//*** Create Catalog Item Request for added groups***//
var addGrp = producer.sna_groups_to_add.toString().split(',');
var remGrp = producer.sna_groups_to_remove.toString().split(',');

if (addGrp != '')
createItem(addGrp, 'Add User to Group');

if (remGrp != '')
createItem(remGrp, 'Remove User from Group');

function createItem(grpArray, addRemove) {
for (var i = 0; i < grpArray.length; i++) {
var userGrp = new GlideRecord('sys_user_group');
userGrp.addQuery('sys_id', grpArray[i]);
userGrp.query();
if (userGrp.next()) {
gs.log('Return Group= ' + userGrp.name); // Get group name
}

var newSI = new GlideRecord('sc_req_item');
newSI.initialize();
newSI.request = newID;
newSI.cat_item = 'a639c9db1bf7cc107b86542f0a4bcb0d'; // ServiceNow Group Access Catalog item
newSI.short_description = addRemove + ' - ' + userGrp.name;
newSI.due_date = gs.endOfThisWeek();
var newSIid = newSI.insert();

//*** Get the variables to be filled out in the User Options of the SI ***//
var itemName = newSI.cat_item.name;
var itemVar = new GlideRecord('item_option_new');
itemVar.addQuery('cat_item.name', itemName);
itemVar.query();
var itemVarList = [];
while (itemVar.next()) {
itemVarList.push(itemVar.getUniqueValue('name'));
}

//*** Populate Options - sc_item_option ***//
for (var x = 0; x < itemVarList.length; x++) {
var itemOption = new GlideRecord('sc_item_option');
itemOption.initialize();
itemOption.order = x;
itemOption.item_option_new = itemVarList[x];

if (itemOption.item_option_new == 'f2390d9f1bf7cc107b86542f0a4bcb6a') { // User requesting:
itemOption.value = newReq.requested_for;
}

if ((addRemove == 'Add User to Group') && (itemOption.item_option_new == '72390d9f1bf7cc107b86542f0a4bcb75')) { // Group to add
itemOption.value = userGrp.sys_id;
}

if ((addRemove == 'Remove User from Group') && (itemOption.item_option_new == '7e390d9f1bf7cc107b86542f0a4bcb6f')) { // Group to remove
itemOption.value = userGrp.sys_id;
}

var optionSysID = itemOption.insert();

//*** Populate Variable Ownerships - sc_item_option_mtom ***//
var itemM2M = new GlideRecord('sc_item_option_mtom');
itemM2M.initialize();
itemM2M.request_item = newSIid; // Parent Item
itemM2M.sc_item_option = optionSysID; // Dependent Value
itemM2M.insert();
}
}
}
gs.addInfoMessage("Thank you! Your request has been submitted - " + newReq.number);
producer.redirect = "sc_request_list.do";

View solution in original post

11 REPLIES 11

Hi,

not much idea on flow designer; but please check below link:

https://docs.servicenow.com/bundle/orlando-servicenow-platform/page/administer/flow-designer/referen...

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

I tried the script you provided but it creates not just requested items, it create the request as well.

I did some research and I think I will be able to use flow designer. But I need your further assistance with scripting.

So this is how it should work:

  1. I have a record producer. The record producer has the following questions:
    • Is this request for someone else?
    • If yes, ask for the employee name
    • Display list collector where it will list the current groups that the above user is currently a member of.
    • Requester can add or remove anything from the list collector.

  2. The record producer, should create the following records:
    • Request (sc_request) 
    • Requested item (sc_req_item) - could be multiple depends on how many groups where selected from the list collector.
    • populate user options (sc_item_option_mtom) - i have a separate catalog item with the following fields.
      • Request for: (reference to sys_user)
      • Group Name: (reference to sys_user_group)

        Note: Each RITM contains one group selected from the list collector.

  3. I will use Flow Designer for the approval of the request and automate adding and removing of the user from the group(s)

This is the script from the record producer. I need help populating the variables:

//*** cancel creation of global record ***//
current.setAbortAction(true);

//*** Create Service Catalog Request  ***//
var newReq = new GlideRecord('sc_request');
newReq.initialize();
newReq.opened_by = gs.getUserID();
newReq.requested_for = producer.request_is_for;
newReq.due_date = gs.endOfThisWeek();
newReq.short_description = 'ServiceNow Access Request';
var newID = newReq.insert();

//*** Create Catalog Item Request ***//
var groupList = producer.sn_grp_group_add_remove.toString(); // list collector
var list = groupList.split(',');
for (var i = 0; i < list.length; i++) {
var newSI = new GlideRecord('sc_req_item');
newSI.initialize();
newSI.request = newID;
newSI.cat_item = '2751e6731ba3c890a730964ead4bcb84'; // ServiceNow Access
newSI.short_description = 'Add User to Group - ';
var newSIid = newSI.insert();
}

//*** Populate variables ***//
//*** 1. request_for
//*** 2. group

 

Thank you in advance.

 

 

Saurabh singh4
Kilo Guru

 

 

 

Hi Jleong

Please go through this, It will give you some clarity

 

 

1. On the Request table, setup a List Collector referencing the User table (I called it:  Also Requested For).

2.  on the Request table, setup a checkbox field (I called it:  Copy Completed).

3.  on the Request table, create a before Business Rule with the following parameters:

find_real_file.png

Use the following script.  Take note of some of my notes in the script, as they directly address some of the issues you are seeing.

 

(function executeRule(current, previous /*null when async*/) {
	
	var copyList = current.u_also_requested_for;
	var copyArray = copyList.split(",").sort();
	var arrayUtil = new ArrayUtil();
	for (u = 0; u < copyArray.length; u++)
		{
		//this was using Incident to test validity of code
		//	var gr = new GlideRecord('incident');
		//gr.initialize();
		//gr.caller_id = copyArray[u]; //using the slice of the array [u]
		//gr.insert();
		
		//Create the extra Request for each person in 'Also Requested For'
		var gr = new GlideRecord('sc_request');
		gr.initialize();
		gr.requested_for = copyArray[u]; //using the slice of the array [u]
		gr.parent = current.sys_id;  //all copied have a Parent
		gr.opened_by = current.opened_by;
		gr.insert();
		gs.log("Also Request For Create Records BR run: " + current.number + ' ' + gr.number + ' requested for ' + gr.requested_for.getDisplayValue());
			
		//Find the Requested Item children of the current Request
		var items = new GlideRecord('sc_req_item');
		items.addQuery('request', current.sys_id);
		items.query();
		//Create Requested Item child for each found
		while (items.next()){
			var citems = new GlideRecord('sc_req_item');
			citems.initialize();
			citems.request = gr.sys_id;
			citems.requested_for = copyArray[u];
			citems.cat_item = items.cat_item;
			citems.insert();
			
			var TheItem = items.cat_item.toString();
			
			var tecvar = new GlideRecord('io_set_item');
			tecvar.addQuery('sc_cat_item',TheItem);
			tecvar.query();
			
			//Put catalog item "'Request other equipment'" variable sets list to array  OtherEquipmentVariableSetsList
			var OtherEquipmentVariableSetsList = [];
			while (tecvar.next())
				{
					OtherEquipmentVariableSetsList.push(tecvar.getValue('variable_set'));
						}
			
			//gs.log("lotg:" + "OtherEquipmentVariableSetsList =" + OtherEquipmentVariableSetsList);
			//Search for stored variables for current request
			
			var vgr = new GlideRecord('sc_item_option_mtom');
			vgr.addQuery('request_item',items.sys_id);
			vgr.query();
			while (vgr.next()){
				vgr.request_item = citems.sys_id;
				vgr.insert();
			}
			
			//Update the RITM to cause Start Workflow BR to trigger
			citems.description = '.';
			citems.update();
			
			
			
			
			
			/**
			//meant to copy the variables, modified from BR to copy variables from RITM to Catalog Task
			var questions = [];
			var catalogItem = items.cat_item.toString();
			var variableSets = [];
			
			var getVariableSets = new GlideRecord('io_set_item');
			getVariableSets.addQuery('sc_cat_item',catalogItem);
			getVariableSets.query();
			while(getVariableSets.next()) {
				variableSets.push(getVariableSets.getValue('variable_set'));
			}
			
			var getCatalogVariables = new GlideRecord('item_option_new'); getCatalogVariables.addQuery('cat_item='+catalogItem+'^active=true^NQvariable_set.sys_idIN'+variableSets.toString()+'^active=true');
			getCatalogVariables.query();
			while(getCatalogVariables.next()) {
				questions.push(getCatalogVariables.getValue('sys_id'));
			}
			
			var variablesCount = questions.length;
			var currentTaskID = items.sys_id.toString();
			
			//this part creates the variables
			for(var i=0;i<variablesCount;i++)
				{
				var getTaskVariables = new GlideRecord('sc_item_variables_task');
				getTaskVariables.addQuery('task',currentTaskID);
				getTaskVariables.addQuery('variable',questions[i]);
				getTaskVariables.setLimit(1);
				getTaskVariables.query();
				if(!getTaskVariables.hasNext())
					{
					getTaskVariables.initialize();
					getTaskVariables.setValue('task',currentTaskID);
					getTaskVariables.setValue('variable',questions[i]);
					getTaskVariables.insert();
				}
			}**/
			
			
			
			
		}
		
		
		
		
	}
	
	current.u_copy_completed = true;
	
})(current, previous);

4.  In my case I needed this for an Order Guide, so I created one specific to when people needed to request the same thing for multiple people.

5.  In that Order Guide, I needed a way to get the other users.  I did so via a Variable Set (in theory so I could use this anywhere).

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

 

 

6.  The Request workflow needed to be updated.  You may need to adjust this to your specific situation:

find_real_file.png

 

The Run Script:  Get Also Requested For, use this script:

var critm = new GlideRecord ('sc_req_item');
critm.addQuery('request', current.sys_id);
critm.orderByDesc('number');
critm.setLimit(1);
critm.query();
if (critm.next()){
	if (critm.variables.copy_user_list != ''){
	current.u_also_requested_for = critm.variables.copy_user_list;
	//current.update();
	}
}

 

Please mark my comment helpful and correct.

Saurabh

Hi Saurabh,

I appreciate your time and assistance. I will check this for sure. 

Regards,

Jocelyn

 

Hi Saurab,

Thanks for sharing your script. It gave me ideas and helped me with what I need to accomplish.

I was able to create a catalog item for each list collector from a record producer. Attached is the script.

I also used Flow Designer to automate the fulfillment.

 

//*** cancel creation of global record ***//
current.setAbortAction(true);

//*** Create Service Catalog Request (SR) ***//
var newReq = new GlideRecord('sc_request');
newReq.initialize();
newReq.opened_by = gs.getUserID();
newReq.due_date = gs.endOfThisWeek();
newReq.short_description = 'ServiceNow Group Access Request';
if (producer.sna_request_for_other == 'no') {
newReq.requested_for = gs.getUserID();
} else {
newReq.requested_for = producer.sna_request_is_for;
}
var newID = newReq.insert();


//*** Create Catalog Item Request for added groups***//
var addGrp = producer.sna_groups_to_add.toString().split(',');
var remGrp = producer.sna_groups_to_remove.toString().split(',');

if (addGrp != '')
createItem(addGrp, 'Add User to Group');

if (remGrp != '')
createItem(remGrp, 'Remove User from Group');

function createItem(grpArray, addRemove) {
for (var i = 0; i < grpArray.length; i++) {
var userGrp = new GlideRecord('sys_user_group');
userGrp.addQuery('sys_id', grpArray[i]);
userGrp.query();
if (userGrp.next()) {
gs.log('Return Group= ' + userGrp.name); // Get group name
}

var newSI = new GlideRecord('sc_req_item');
newSI.initialize();
newSI.request = newID;
newSI.cat_item = 'a639c9db1bf7cc107b86542f0a4bcb0d'; // ServiceNow Group Access Catalog item
newSI.short_description = addRemove + ' - ' + userGrp.name;
newSI.due_date = gs.endOfThisWeek();
var newSIid = newSI.insert();

//*** Get the variables to be filled out in the User Options of the SI ***//
var itemName = newSI.cat_item.name;
var itemVar = new GlideRecord('item_option_new');
itemVar.addQuery('cat_item.name', itemName);
itemVar.query();
var itemVarList = [];
while (itemVar.next()) {
itemVarList.push(itemVar.getUniqueValue('name'));
}

//*** Populate Options - sc_item_option ***//
for (var x = 0; x < itemVarList.length; x++) {
var itemOption = new GlideRecord('sc_item_option');
itemOption.initialize();
itemOption.order = x;
itemOption.item_option_new = itemVarList[x];

if (itemOption.item_option_new == 'f2390d9f1bf7cc107b86542f0a4bcb6a') { // User requesting:
itemOption.value = newReq.requested_for;
}

if ((addRemove == 'Add User to Group') && (itemOption.item_option_new == '72390d9f1bf7cc107b86542f0a4bcb75')) { // Group to add
itemOption.value = userGrp.sys_id;
}

if ((addRemove == 'Remove User from Group') && (itemOption.item_option_new == '7e390d9f1bf7cc107b86542f0a4bcb6f')) { // Group to remove
itemOption.value = userGrp.sys_id;
}

var optionSysID = itemOption.insert();

//*** Populate Variable Ownerships - sc_item_option_mtom ***//
var itemM2M = new GlideRecord('sc_item_option_mtom');
itemM2M.initialize();
itemM2M.request_item = newSIid; // Parent Item
itemM2M.sc_item_option = optionSysID; // Dependent Value
itemM2M.insert();
}
}
}
gs.addInfoMessage("Thank you! Your request has been submitted - " + newReq.number);
producer.redirect = "sc_request_list.do";