- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2023 03:00 AM - edited 10-02-2023 03:09 AM
Hi,
We got the record created vis POST method , but now need to update the variable of existing RITM.
Tried some method shared in community and created a Scripted REST API, but seems I am doing something wrong as it is not working.
Please advise.
Script REST API created:
Error:
Regards,
B
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2023 04:25 AM - edited 10-02-2023 04:25 AM
@Bijender You are mentioning RITM number in relative path and in the script you are trying to fetch it from query parameter which is wrong. You need to fetch it from path parameter. Follow below steps
1) Put the below script in script section
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var ritmNumber = request.pathParams.number;
var reqBody = request.body.data;
if (ritmNumber) {
var bodyObject = JSON.stringify(reqBody);
var parser = JSON.parse(bodyObject);
var variables = parser.variables; // get the variables data
var variablesObject = JSON.stringify(variables);
var variableElements = JSON.parse(variablesObject); //parse variables data
var ritmObj = new GlideRecord('sc_req_item');
if (ritmObj.get('number', ritmNumber))
{
gs.info(ritmObj.ritmNumber);
ritmObj.variables.new_email = variableElements.new_email;
var recordUpdated = ritmObj.update();
}
if (recordUpdated)
{
var body = {};
body.status = 'record successfully updated';
response.setBody(body); // set the response body
}
}
})(request, response);
2) As you are already passing the number in path parameter the no need pass again in the request body. Sample request body as below
{
variables: {
"new_email": "servicenow12345@gmail.com"
}
}
If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2023 03:30 AM
Strange...
It looks like somehow postman is sneding a get, instead of a put. Doesn't seem to be a ServiceNow issue.
Does it work when you use Rest API Explorer within ServiceNow?
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2023 03:44 AM
Hi Peter,
When I use REST API Explorer within service-now it gives me 200 success but response body as blank.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2023 03:52 AM
Hi @Bijender,
Thats good, so the first issue is something within Postman, can't really help with that.
You are not getting a response because you haven't defined that in the Script.
Use something like this:
var bodyArray = [];
var body = {};
body.name = "incident";
body.number = "1234";
body.caller = {"id":"user1"};
bodyArray.push(body);
response.setBody(bodyArray);
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2023 04:25 AM - edited 10-02-2023 04:25 AM
@Bijender You are mentioning RITM number in relative path and in the script you are trying to fetch it from query parameter which is wrong. You need to fetch it from path parameter. Follow below steps
1) Put the below script in script section
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var ritmNumber = request.pathParams.number;
var reqBody = request.body.data;
if (ritmNumber) {
var bodyObject = JSON.stringify(reqBody);
var parser = JSON.parse(bodyObject);
var variables = parser.variables; // get the variables data
var variablesObject = JSON.stringify(variables);
var variableElements = JSON.parse(variablesObject); //parse variables data
var ritmObj = new GlideRecord('sc_req_item');
if (ritmObj.get('number', ritmNumber))
{
gs.info(ritmObj.ritmNumber);
ritmObj.variables.new_email = variableElements.new_email;
var recordUpdated = ritmObj.update();
}
if (recordUpdated)
{
var body = {};
body.status = 'record successfully updated';
response.setBody(body); // set the response body
}
}
})(request, response);
2) As you are already passing the number in path parameter the no need pass again in the request body. Sample request body as below
{
variables: {
"new_email": "servicenow12345@gmail.com"
}
}
If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2023 12:53 AM
@Bijender If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!