Create multiple RITM under single request depending upon list collector values through workflow

Vishal Birajdar
Giga Sage

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

VishalBirajdar_0-1696221780895.png

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

VishalBirajdar_1-1696221780918.png

C] sc_item_option_mtom

VishalBirajdar_2-1696221780935.png

 

Now let’s do some development.

 

Step 1 :  Create two new variables Catalog Item - "Development Laptop (PC)"
First variable
**Second variable
Name       : User List
Type       : List Collector
List Table : User (sys_user)

Name          : Hidden Variable
Type          : Select box
Choices       : 1.Yes 2.No
Default Value : Yes
** 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 :

VishalBirajdar_3-1696221780952.png

At portal it should be look like below :

VishalBirajdar_4-1696221780962.png

 

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.

VishalBirajdar_8-1696222835588.png

 

Step 3 : Open the Workflow in workflow editor.
A] Add new  “Switch” workflow activity in workflow on variable “Hidden variable”  as shown in below

VishalBirajdar_11-1696223040915.png

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

VishalBirajdar_12-1696223040922.png

 

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)

VishalBirajdar_13-1696223474056.png

2.Request summary should look like this :

VishalBirajdar_14-1696223474062.png

3.We will have 3 ritms  (RITM number will be different in your case ) under one request with separate
workflow attached.


VishalBirajdar_15-1696223474070.png

1.RITM0010014 – Main RITM

VishalBirajdar_16-1696223474076.png

Workflow (RITM0010014):

VishalBirajdar_17-1696223474084.png

2.RITM0010015 – RITM created through main workflow

VishalBirajdar_18-1696223474090.png

Workflow –(RITM0010015)

VishalBirajdar_19-1696223474100.png

Same with the RITM0010016 

 

 

References :

1 . ServiceNow Documentation link

2 . Community reply by @Brad Bowman -  link 

 

Any suggestions, amendment's are welcome....!!!

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates
12 REPLIES 12

Abhijeet_Pawar
Tera Guru

Helpful article, thank you, @Vishal Birajdar.

Thank you...!! @Abhijeet_Pawar 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Manikandan2702
Tera Contributor

Very Helpful

Gunjan soni1
Tera Contributor

Hi Vishal,

I have written the same script but in my case it is creating multiple ritms.

Gunjansoni1_0-1746612139536.png

Please have a look.