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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Can you please explain where the list collector variable would be present? Are you saying you are having 1 catalog item with list collector and when user selects 5 values in list collector you want 5 RITMs to be created.

Regards
Ankur

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

Hi Ankur! Thanks for responding to my email.

Yes, 1 catalog item with list collector and it will create RITMs based on values from the list collector.

 

Hi,

so you can have workflow and run script to create multiple RITMs

var listValue = current.variables.<listVariable>.toString().split(',');

for(var i=0;i<listValue.length;i++){

// ensure you give your variable names

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
 //add your requested item to the cart by sys_id of the catalog item
var item = cart.addItem('4054428fdb151f0097679ec6db9619c0', 1);

//fill in the variables on the request item form
cart.setVariable(item,"u_requested_for", "e80edd6edbec6bc097275e25ca9619a4"); 
cart.setVariable(item,"contact_number", "0");

cart.setVariable(item,"short_description", "hello");
cart.setVariable(item,"description", "hello");
var rc = cart.placeOrder();

}

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

Could we do this using Flow Designer?