Scripted Rest API not giving correct response
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2024 02:22 AM
Hi All,
I am facing a issue in the Scripted Rest API where our resource we have used JSON.parse to drive our custom output from Approval, Requested Item and Change Request tables. When we try to hit our API, it returns the below error.
I transferred the same Scripted Rest API to our PDI and tested it. It worked as intended there.
1. I'd want to ask what the most likely reason for the following issue that we are experiencing.
2. Also, Upgrading our instance to Washington and then reverting back it to Vancouver could have any impact on it, because prior to that the Scripted Rest API was working fine.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2024 02:41 AM
Hi,
This error is observed mostly in JSON.parse command.
You can trouble shoot the script by adding log before the parse command to ensure that is the root cause. If possible share the script and sample payload to have a look
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2024 02:58 AM
Hi Palani,
We have added log before the JSON.parse and tried to print the response.getBody and it was showing up the below response.
Scripted Rest Resource Script
var restMsg = new sn_ws.RESTMessageV2();
var endpointurl = baseUrl + 'api/now/table/' + reftable;
restMsg.setEndpoint(endpointurl);
restMsg.setHttpMethod('GET');
restMsg.setQueryParameter("sysparm_query", "approver=" + callerValidation.callersys_id + "^state=requested^sysapproval.sys_class_name=sc_req_item^ORsysapproval.sys_class_name=change_request");
restMsg.setQueryParameter("sysparm_display_value", "true");
restMsg.setQueryParameter("sysparm_exclude_reference_link", "true");
restMsg.setQueryParameter("sysparm_suppress_pagination_header", "true");
restMsg.setQueryParameter("sysparm_fields", "approver,sysapproval.sys_class_name,sys_id");
restMsg.setAuthenticationProfile('basic', basicAuthProfile);
restMsg.setRequestHeader("Accept", "application/json");
restMsg.setRequestHeader('Content-Type', 'application/json');
// var requestBodyJson = {};
// restMsg.setRequestBody(JSON.stringify(requestBodyJson));
var restRes = restMsg.execute();
output.status_code = (JSON.stringify(restRes.getStatusCode()));
output.status_message = restRes.getBody();
var restResObj = JSON.parse(output.status_message);
var recordSys_id = [];
for (var i = 0; i < restResObj.result.length; i++) {
recordSys_id.push(restResObj.result[i].sys_id);
}
var glideRapproval = new GlideRecord('sysapproval_approver');
glideRapproval.addEncodedQuery('sys_idIN' + recordSys_id + "^sysapproval.sys_class_name=sc_req_item");
glideRapproval.query();
var ritmCount = glideRapproval.getRowCount();
var glideCapproval = new GlideRecord('sysapproval_approver');
glideCapproval.addEncodedQuery('sys_idIN' + recordSys_id + "^sysapproval.sys_class_name=change_request");
glideCapproval.query();
var changeCount = glideCapproval.getRowCount();
var count = {
"Total Count": ritmCount + changeCount,
"RITM Count": ritmCount,
"Change Count": changeCount
};
respObj.body = count;
response.setBody(respObj);
response.setStatus(200);
return response;
We have tested the above mentioned script in our PDi as well and there is is working fine.
Sample Payload:
{
"result": {
"body": {
"Total Count": 20.0,
"RITM Count": 9,
"Change Count": 11
}
}
}