Ashish Nahar
Kilo Guru

Many of my customers admins and developers reached out to understand how we pass referenced variables using OOB API's provided by ServiceNow. Most of them have been finding it difficult to pass the values to a catalog item. When they pass a payload with display value on a reference variable, the output is always empty. 

I decided to write this article and show you a very easy method of achieving this by adding few lines in the script.

Before we proceed in to this, you all must be aware that ServiceNow uses Scripted rest services to define all the resources which can be used by third party to perform actions on Service Catalog.

In your instance go to the following path 

https://{your instance name}.service-now.com/sys_ws_definition.do?sys_id=b4558e83c3a302006f333b0ac3d3ae8e

this should take you the "Service Catalog API" record. Make sure you have your update set created in the correct application scope. (Manage Update Set)

Under resources look for a specific resource "Buy Item" (We will use this resource to make changes and see the referenced variable being updated)

Note: I would recommend creating a new resource like Buy Item (copy it and relabel the name and path) as it avoids any customization.

In our example the third-party application wants to open a request on behalf of someone and requested for variable is available on catalog item to cater for that. Since requested for variable is referencing user table it requires sys_id of the user to be passed in the body of the payload. This customer can only provide email address in the body.

To create a catalog item with requested for filled using API, the following line of code should be added. This will help you identify the data passed in the body and define value to the data before creating the catalog item.

// You can definitely be smarter in scripting and use the best method possible to get the user info.

//Finding the user
var source = request_body.variables;
var user = new GlideRecord('sys_user');
if(user.get('email',source['requested_for'])){
source['requested_for'] = user.sys_id.toString();
}

 

By using the above, i am now replacing the data in body from email address to sys_id of the user and the result should be seen in the requested item upon submission.

 

Body sent by third party:

{'sysparm_quantity':1,

variables:{

'requested_for':'ashish.nahar@xxxxxxxx.com',

'short_description':'Ashish Test',

'describe_your_request':'This is to test again'

}

}

Response:

{

  "result": {

    "sys_id": "5072074b871d8d104a9487f90cbb35bf",

    "number": "REQ0283951",

    "request_number": "REQ0283951",

    "request_id": "5072074b871d8d104a9487f90cbb35bf",

    "table": "sc_request"

  }

}


On the Requested item

find_real_file.png




I hope this article will be of help to all.

Comments
Anil Lande
Kilo Patron

@Ashish Nahar Nice Article. It would be helpful if you update article with hyperlinks. All the links added are as text. Please use hyperlink option to add links.

find_real_file.png

 

Thanks,

Anil Lande

Ashish Nahar
Kilo Guru

Thank you for this. Pretty new to writing articles 🙂

John Andre See
Tera Contributor

hi Ashish, how did u manage to update the variables? I've wrote below script but the variable is not updating even if the request is successful, tried POST/PUT/PATCH

 

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    var pathParams = request.pathParams;
    var obj = pathParams.number; // [false] 
    var ritm = new GlideRecord("sc_req_item");

    var reqbody = request.body;
    var reqdata = reqbody.data;

    if (obj && obj != '') {
        ritm.addQuery("number", obj);
        ritm.query();
        if (ritm.next()) {
            ritm.variables.u_name = reqdata.variables.mgmt;
            ritm.work_notes = "owrk"; //Parameter means the key in JSON
			ritm.update();
        }
    }

})(request, response);

 

 

Ashish Nahar
Kilo Guru

@John Andre See  Apologies, i have been away from the community for quite some time now. Reaching out if you got all you wanted or need some other help.

Version history
Last update:
‎02-09-2022 05:01 AM
Updated by: