Rest API to order Multiple Items

Michael51
Tera Guru

Hello All,

I have an requirement to create/order Multiple Items as part of our intergation and this is going to be an Inbound Integration , we have 5 items 

order laptop

Access to VPN 

order Mobile 

Order ID 

Order keyboard - in this Item I have to set predefined variable values 

1. Automatic key board - choice to Yes 

we cannot do this as order guide , we need to create as Individual request 

So now I need to create 5 Items via Rest API can you please suggest some solution and Sample script ,  

@Ankur Bawiskar @Voona Rohila @jaheerhattiwale 

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Michael51 

I believe you want all 4/ 5 RITMs in same REQ

I created blog for this couple of years ago. have a look

If it helps please mark the blog as helpful

Multiple RITMs in same Request Using CartAPI 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

jaheerhattiwale
Mega Sage
Mega Sage

@Michael51 Create a scripted rest api to add the custom code to it and trigger the api from outside the servicenow as you wish.

 

Send the Request body to the api like below 

[
		{
			"catalog_item_sys_id": "<order laptop CAT ITEM SYS ID HERE>"
		},
		{
			"catalog_item_sys_id": "<Access to VPN CAT ITEM SYS ID HERE>"
		},
		{
			"catalog_item_sys_id": "<order Mobile CAT ITEM SYS ID HERE>"
		},
		{
			"catalog_item_sys_id": "<Order ID CAT ITEM SYS ID HERE>"
		},
		{
			"catalog_item_sys_id": "<Order keyboard CAT ITEM SYS ID HERE>",
			"<Automatic key board VARIABLE NAME HERE>": "Yes"
		}
	]

 

Steps:

1. Go to "Scripted rest api" module

jaheerhattiwale_0-1685597484769.png

 

2. Create new scripted rest api

jaheerhattiwale_1-1685597508200.png

 

3. Scroll down to see the related list

jaheerhattiwale_2-1685597568295.png

 

4. create a new scripted rest resource

jaheerhattiwale_3-1685597667073.png

 

5. Add below code to scripted rest resource

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var requestBody = request.body.data;
    for (var i = 0; i < requestBody.length; i++) {
        var cart = new Cart();
        var item = cart.addItem(requestBody[i].catalog_item_sys_id);

        var keys = Object.keys(requestBody[i]);

        for (var j = 0; j < keys.length; j++) {
            if (keys[j] != "catalog_item_sys_id") {
                cart.setVariable(item, keys[j], requestBody[i][keys[j]]);
            }
        }

        var placeOrder = cart.placeOrder();
    }

})(request, response);

 

Please mark as correct answer if this solves your issue.

 

 

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

13 REPLIES 13

@Michael51 

you can use flow designer step Submit catalog item and also there is a step for Inbound action

you need to create inbound email flow

Create an Inbound Email Flow 

Submit Catalog Item Request action 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

jaheerhattiwale
Mega Sage
Mega Sage

@Michael51 Create a scripted rest api to add the custom code to it and trigger the api from outside the servicenow as you wish.

 

Send the Request body to the api like below 

[
		{
			"catalog_item_sys_id": "<order laptop CAT ITEM SYS ID HERE>"
		},
		{
			"catalog_item_sys_id": "<Access to VPN CAT ITEM SYS ID HERE>"
		},
		{
			"catalog_item_sys_id": "<order Mobile CAT ITEM SYS ID HERE>"
		},
		{
			"catalog_item_sys_id": "<Order ID CAT ITEM SYS ID HERE>"
		},
		{
			"catalog_item_sys_id": "<Order keyboard CAT ITEM SYS ID HERE>",
			"<Automatic key board VARIABLE NAME HERE>": "Yes"
		}
	]

 

Steps:

1. Go to "Scripted rest api" module

jaheerhattiwale_0-1685597484769.png

 

2. Create new scripted rest api

jaheerhattiwale_1-1685597508200.png

 

3. Scroll down to see the related list

jaheerhattiwale_2-1685597568295.png

 

4. create a new scripted rest resource

jaheerhattiwale_3-1685597667073.png

 

5. Add below code to scripted rest resource

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var requestBody = request.body.data;
    for (var i = 0; i < requestBody.length; i++) {
        var cart = new Cart();
        var item = cart.addItem(requestBody[i].catalog_item_sys_id);

        var keys = Object.keys(requestBody[i]);

        for (var j = 0; j < keys.length; j++) {
            if (keys[j] != "catalog_item_sys_id") {
                cart.setVariable(item, keys[j], requestBody[i][keys[j]]);
            }
        }

        var placeOrder = cart.placeOrder();
    }

})(request, response);

 

Please mark as correct answer if this solves your issue.

 

 

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Hi @jaheerhattiwale , Can’t we include item sysid in our script and set the variable data? as they only send requested info in the payload

@Michael51 We can do that, just the resource code like below

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var requestBody = [{
            "catalog_item_sys_id": "<order laptop CAT ITEM SYS ID HERE>"
        },
        {
            "catalog_item_sys_id": "<Access to VPN CAT ITEM SYS ID HERE>"
        },
        {
            "catalog_item_sys_id": "<order Mobile CAT ITEM SYS ID HERE>"
        },
        {
            "catalog_item_sys_id": "<Order ID CAT ITEM SYS ID HERE>"
        },
        {
            "catalog_item_sys_id": "<Order keyboard CAT ITEM SYS ID HERE>",
            "<Automatic key board VARIABLE NAME HERE>": "Yes"
        }
    ];

    for (var i = 0; i < requestBody.length; i++) {
        var cart = new Cart();
        var item = cart.addItem(requestBody[i].catalog_item_sys_id);

        var keys = Object.keys(requestBody[i]);

        for (var j = 0; j < keys.length; j++) {
            if (keys[j] != "catalog_item_sys_id") {
                cart.setVariable(item, keys[j], requestBody[i][keys[j]]);
            }
        }

        var placeOrder = cart.placeOrder();
    }

})(request, response);

 

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Hi @jaheerhattiwale , 

Requirement was bit changed , they need only Request with 5 RITM’s

  • How can I enhance following script to create this logic
    also just confirming they just send requested for in the payload , remaining everything we need to handle in the logic , can you help me here
  • 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);