Create multiple RITM under single request depending upon list collector values through workflow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2023 10:43 PM
Hi Team,
This might be helpful for someone who want to learn something new.
Use Case : Create multiple RITM under single request depending upon list collector values
through workflow without using CartAPI.
Pre-requisites : 1. PDI
2. Catalog Item - "Development Laptop (PC)" [OOTB catalog item available on PDI]
Description : When end user selects the multiple users (let say 3) in “User list” then 3 RITM to
be generated under single request.It should calculate the price of all 3 RITM on portal.
Note : Be familiar with three tables “item_option_new” , “sc_item_option” & “sc_item_option_mtom”
1.When we create variable it will be store in table “item_option_new” .
we will require this table in upcoming steps.
A] item_option_new
2.When end user fills the variables from portal it will create a record in “sc_item_option” and it will
be unique with each ritm by creating record in “sc_item_option_mtom”table
B] sc_item_option
C] sc_item_option_mtom
Now let’s do some development.
Step 1 : Create two new variables Catalog Item - "Development Laptop (PC)"
First variable | **Second variable |
Name : User List | Name : Hidden Variable |
** Second variable will be hidden & we will use it to divert the workflow so that it will not go in infinite
loop.
Catalog item should look like this :
At portal it should be look like below :
Step 2 : Copy the existing workflow from this catalog item [we will update the copied workflow.]
Workflow : Copy of Procurement Process Flow – Hardware
Attach it to the catalog.
Step 3 : Open the Workflow in workflow editor.
A] Add new “Switch” workflow activity in workflow on variable “Hidden variable” as shown in below
B] Add new activity “Run script” with any name – Put this as blank for now , we will write script to create
multiple RITM using this script activity.
Connect “No” with “Approval action” & “Yes” with Run script activity of switch
Connect Run script activity to approval action.
It should look like this
Step 4 : Let’s write script in Run script activity in workflow
/* ============Start of run script activity===============*/
/* 1. Get values from user list variable */
var list = current.variables.user_list.toString();
var array = list.split(',');
/* 2. Get current ritm’s request this will be used to set request on multiple Ritm to be created*/
var request = current.request;
/* 3. Store sys_id of our catalog item */
var catalogItem = '3cecd2350a0a0a6a013a3a35a5e41c07'; // Not to hardcode (use system property)
//var catalogItem = gs.getProperty(‘name of property where sysid of cat item stored’);
/* 4. loop through array we have created in 1 and create RITM */
/* Loop will initialize from 1 because we have creating one ritm from main request. */
for (var i = 1; i < array.length; i++) {
/* 4a. Create RITM*/
var grRitm = new GlideRecord("sc_req_item");
grRitm.initialize();
grRitm.cat_item = catalogItem;
grRitm.price = current.price;
grRitm.request = request;
grRitm.requested_for = array[i];
grRitm.due_date = current.due_date;
//Store the sys Id in another variable
var ritmSysId = grRitm.insert();
/*End of RITM creation*/
/* 4b. Store the variables from catalog Item in an array */
var itemName = grRitm.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'));
}
/*End of Store the variables from catalog Item in an array */
/* 4c.Create a records in sc_item_option */
/* This table contains value of variable filled by end user , In this case we will update it through script */
for (var j = 0; j < itemVarList.length; j++) {
var itemOption = new GlideRecord('sc_item_option');
itemOption.initialize();
itemOption.order = j;
itemOption.item_option_new = itemVarList[j];
// sys_id of variable "Hidden variable"
if(itemOption.item_option_new =='1c4f7a252fe17110cb5554492799b62d'){
itemOption.value = 'No'; // set the value to "No" so flow will divert and not get into loop
}
// sys_id of variable "User list"
if(itemOption.item_option_new =='6cbe3a252fe17110cb5554492799b6e3'){
itemOption.value = array[i];
}
// Store the sysid
var optSysID = itemOption.insert();
/* 4d. Create a relationship of RITM with variables value */
var ritmM2M = new GlideRecord('sc_item_option_mtom');
ritmM2M.initialize();
ritmM2M.request_item = ritmSysId; // Parent Item
ritmM2M.sc_item_option = optSysID; // Dependent Value
ritmM2M.insert();
}
/* 4e. Start the workflow (sys_id of workflow from "wf_workflow" table “Copy of Procurement Process Flow - Hardware”)*/
startWorkflow('5c7efae12fe17110cb5554492799b62b');
/*for startWorkflow() – Taken reference from OOTB Business rule from RITM table
Business rule : Start Workflow
*/
}
/* 4. End of loop through array we have created in 1 and create RITM */
/* Attach workflow to Ritm */
function startWorkflow(id) {
var grRitm1 = new GlideRecord("sc_req_item");
grRitm1.addQuery("sys_id", ritmSysId);
grRitm1.query();
if (grRitm1.next()) {
var w = new Workflow();
var context = w.startFlow(id, grRitm1, grRitm1.operation(), getVars(grRitm1));
if (context != null)
grRitm1.context = context.sys_id;
workflow.info("context = " +grRitm1.context);
}
grRitm1.update();
}
/* Send the RITM variables to workflow*/
function getVars(grRitm1) {
var vars = {};
for (var n in grRitm1.variables)
vars[n] = grRitm1.variables[n];
return vars;
}
/*Update User list for main (current) RITM*/
current.variables.user_list = array[0];
current.requested_for = array[0];
/*====================End of run script Activity===============================*/
Validate & Publish the workflow.
Its time to test it.
Output & Test Case :
1.Select any random users on form & submit the request (Order now)
2.Request summary should look like this :
3.We will have 3 ritms (RITM number will be different in your case ) under one request with separate
workflow attached.
1.RITM0010014 – Main RITM
Workflow (RITM0010014):
2.RITM0010015 – RITM created through main workflow
Workflow –(RITM0010015)
Same with the RITM0010016
References :
1 . ServiceNow Documentation link
2 . Community reply by @Brad Bowman - link
Any suggestions, amendment's are welcome....!!!
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- 5,730 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2023 10:48 PM
Helpful article, thank you, @Vishal Birajdar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2023 11:12 PM
Thank you...!! @Abhijeet_Pawar
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 04:53 AM
Very Helpful
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2025 03:03 AM
Hi Vishal,
I have written the same script but in my case it is creating multiple ritms.
Please have a look.