Ankur Bawiskar
Tera Patron
Tera Patron

Many a times you would require script to create multiple RITMs for same Catalog Item under single Request..

Below script would help you create the same; I have created a catalog item with 5 variables. Using the below script it would create 2 RITMs as the json contains 2 objects & both the RITMs will have same request:

Scenario: You are receiving a json data from some external integration & want to create RITMs to be placed under a single REQ so that they can be tracked.

Sample JSON:

[
  {
    "requested_for": "Abel Tuter",
    "requested_by": "Sam Jone",
    "model": "Hyundai",
    "quantity": 1,
    "device_model_name": "Car-Petrol"
  },
  {
    "requested_for": "Fred Luddy",
    "requested_by": "Amy Jone",
    "model": "Honda",
    "quantity": 1,
    "device_model_name": "Car-Diesel"
  }
]

 Catalog Item:

find_real_file.png

 

Script Below:

var catalogItem = 'b70c34e807334c10540bf2508c1ed073'; // catalog item sys_id

var jsonArray = [{"requested_for":"Abel Tuter","requested_by":"Sam Jone","model":"Hyundai","quantity":1,"device_model_name":"Car-Petrol"},{"requested_for":"Fred Luddy","requested_by":"Amy Jone","model":"Honda","quantity":1,"device_model_name":"Car-Diesel"}];

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);

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

var jsonObj = jsonArray[i];
var parser = JSON.parse(JSON.stringify(jsonObj));

var item = cart.addItem(catalogItem,1);
cart.setVariable(item, 'requested_for', parser.requested_for);
cart.setVariable(item,'requested_by',parser.requested_by);
cart.setVariable(item,'model', parser.model);
cart.setVariable(item,'quantity', parser.quantity);
cart.setVariable(item,'device_model_name', parser.device_model_name);

}

var rc = cart.placeOrder();
gs.info('Request Number is: ' + rc.number);

Output: 2 RITMs for same Request REQ0010217

find_real_file.png

1st RITM:

find_real_file.png

2nd RITM:

find_real_file.png

Note: Currently this script would work only in global scope.

Thanks for reading the blog and do provide your inputs/suggestions if any.

Hope you find this article helpful. Don’t forget to Mark it Helpful, Bookmark.
Thanks,
Ankur Bawiskar

ServiceNow MVP 2020,2019,2018

My Articles & Blogs

5 Comments