Create RITM using cart api in servicenow

39

This article help you to create RITM using Cart API Like

  1. Create RITM using inbound email.
  2. Create RITM using Business Rule.
  3. Create RITM using Script Include.

 

In ServiceNow, creating and placing an order for catalog items programmatically is essential for automation and integrating Service Catalog functionality within applications.

There are two different way for achieving this:

    1. Using the Cart API (global scope).
    2. Using the CartJS API (scoped application).

1. Using the Cart API (global scope): Cart API is server-side script which you can use in all server type in ServiceNow like in Inbound Action, Business Rule, Script Include and so on… This is best for global application scope.

var id = GlideGuid.generate(null); // Generate a unique GUID for the cart
var cart = new Cart(id); // Instantiate a new cart with the GUID

var item = cart.addItem('Put sys_id of your catalog ite.'); 
cart.setVariable(item,'put your variable name ','your varibale value');
cart.setVariable(item,'put your variable name2 ','your varibale value2');
var request = cart.placeOrder(); 

//Example...
// Add the catalog item using its sys_id
var RITM = cart.addItem('060f3afa3731300054b6a3549dbe5d3e'); // sys_id of the catlog item

// Set variables for the catalog item
cart.setVariable(RITM, 'ram', '16');       // RAM size
cart.setVariable(RITM, 'os', 'win10');     // Operating system
cart.setVariable(RITM, 'storage', '1tb');  // Storage capacity

// Place the order
var request = cart.placeOrder(); // Returns a GlideRecord for the generated sc_request

Note: API always returns the Request number and to get the RITM number you need to do a GlideRecord. like below.

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('e2132865c0a8016500108d9cee411699',12);
var rc = cart.placeOrder();

var grRITM = new GlideRecord("sc_req_item");
if(grRITM.get("request", rc.sys_id+"")) {
gs.print(grRITM.number);
}

//In case you are going to add multiple items into the cart before requesting then use following code
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('e2132865c0a8016500108d9cee411699',12);
var rc = cart.placeOrder();

var grRITM = new GlideRecord("sc_req_item");
while(grRITM.get("request", rc.sys_id+"")) {
gs.print(grRITM.number);
}

 

2. Using the CartJS API (Scoped Application): Cart API is server-side script which you can use in all server type in ServiceNow like in Inbound Action, Business Rule, Script Include and so on… This is best for scoped application scope.

Use below code to submit an ritm.

var cart = new sn_sc.CartJS();
var item = {
    'sysparm_id': '060f3afa3731300054b6a3549dbe5d3e', // Catalog item sys_id
    'sysparm_quantity': '1',
    'variables': {
        'ram': '16',
        'os': 'win10',
        'storage': '1tb'
    }
};

// Add item to the cart
var cartVals = cart.addToCart(item); // Returns a JSON object with cart status

// Checkout and submit the order
var checkoutVals = cart.checkoutCart(); // Returns JSON with checkout details

 

3. Choose the Right API for Your Requirement

  • Global Scope: Use the Cart API when you’re outside a scoped environment, such as in custom workflows or server-side scripts.
  • Scoped Context: Use CartJS in scoped applications, where JSON-based structure offers a cleaner and more efficient method to handle cart items and variables.

 

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here