
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 05:34 AM
I recently had a need to allow an external application to make a REST call into ServiceNow to set a catalog variable on a specific RITM. Since there’s not an out of the box REST call for catalog variables, I wrote a scripted REST API to accomplish this. It’s very versatile, so I thought I would share it in case others need something similar.
HOW IT WORKS
The external system makes a PATCH call to https://{yourinstancename}.service-now.com/api/{youridentifier}/set_catalog_variable with a payload like below:
{
"number": "RITM0000001",
"sys_id": "aeed229047801200e0ef563dbb9a71c2",
"variables" : {
"your_variable_name" : "Value to set"
},
"comments": "Whatever comments you want."
}
The script will try to find the Requested Item with the sys_id first. If that doesn’t work, it will try the number field. If it doesn’t find either, it will send back an error that the record was not found.
If it finds the record, it will look for whatever variables that are listed. If they are not found, it will return an error message that the variables were not available on the record. If they are found, it will set them.
The comments are optional, but if they are set, it will add that to the comments of the record.
HOW TO DO IT
- Go to System Web Services -> Scripted Web Services -> Scripted REST APIs.
- Create a new one and give it a name. (I called mine Set Catalog Variable.)
- Save the record.
- Add documentation if you want and change any security settings, if desired.
- Then create a new Resource from the related list.
- Give it a name. (I named mine DEFAULT PATCH.) Set the HTTP Method to PATCH.
- Add the script below to the script field and save the record.
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody, responseBody, status, sm;
var res = {};
try {
requestBody = request.body;
var JSONdata = new global.JSON().decode(requestBody.dataString);
//Get RITM
var ritm = new GlideRecordSecure('sc_req_item');
if (JSONdata.sys_id == '' || JSONdata.sys_id == null) {
ritm.addQuery('number', JSONdata.number);
} else {
ritm.addQuery('sys_id', JSONdata.sys_id);
}
ritm.query();
var variables = [];
if (ritm.next()) {
//set variables
var obj = JSONdata.variables;
for (var key in obj) {
variables.push(key);
if (ritm.variables[key] != undefined) {
ritm.variables[key] = obj[key.toString()];
} else {
return {
"status": "Error",
"message": "Variable " + key + " doesn't exist on record.",
};
}
}
if(JSONdata.comments){
ritm.comments = JSONdata.comments;
}
ritm.update();
return {
"status": "Success",
"message": "Record updated",
};
} else {
return {
"status": "Error",
"message": "Record not found",
};
}
} catch (ex) {
res["status"] = "500";
res["message"] = "An error has occurred";
response.setBody(JSON.stringify(res));
status = '500';
}
})(request, response);
That's it! I hope it's helpful to someone.
Solved! Go to Solution.
- 1,094 Views

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 11:52 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 11:49 AM
Thanks, Jennifer. The steps are very clear!
Could you update this content to an 'Article'.
It shows up as a 'Question'.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 11:52 AM
Unfortunately, I don't appear to have the option to do that.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 11:58 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 12:15 PM - edited 08-20-2024 12:16 PM
I don't have that option. I think you have to have special rights to see those choices. All I can post is Questions. I marked it as Answered, so at least it won't show up as something that needs to be answered. Hopefully that will help.