logo

NJP

Service Catalog REST API - Updating variables via the API call

Import · Feb 09, 2022 · article

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();

}

View original source

https://www.servicenow.com/community/itsm-articles/service-catalog-rest-api-updating-variables-via-the-api-call/ta-p/2300489