- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2019 07:11 AM
Is this the proper way to update the RITM state via scripted rest API? I can update everything else but the state for some reason. The value that is being passed in the API is the state value (i.e. 3 for closed complete).
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var writer = response.getStreamWriter();
var hdrs = {};
var path = request.pathParams;
var instanceName = gs.getProperty('instance_name');
var bodyData = request.body.data;
var ritm = bodyData.ritm;
var sys_id = bodyData.sys_id;
var state = bodyData.state;
//check that all required fields are populated
if (!ritm || !sys_id) {
hdrs['Content-Type'] = 'application/json';
response.setStatus(400);
response.setHeaders(hdrs);
response.setBody('TEST');
//start building response object
writer.writeString("{\"errors\":[");
var errorOBJ = {};
errorOBJ.error_message = 'Please enter all of the required data before executing.';
writer.writeString(global.JSON.stringify(errorOBJ));
//close the response object
writer.writeString("]}");
} else {
//Updates request item
updateRequestItem(sys_id, ritm, state);
}
// ----------------------------------------------------FUNCTIONS-----------------------------------------------------------------------------
function updateRequestItem(ritm, sys_id, state){
var grreq = new GlideRecord("sc_req_item");
grreq.addQuery("number", ritm);
grreq.addQuery("sys_id", sys_id);
grreq.query();
if (grreq.next()) {
grreq.state = state;
grreq.update();
}
response.setStatus(201);
response.setContentType("application/json");
// Use stream writer to send custom response
response.getStreamWriter().writeString('{"result":"Service Request State = Closed Complete"}');
}
})(request, response);
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2019 08:09 AM
The script looks ok but since you are executing the GlideRecord update in the scripted REST block make sure that the users who is authenticating has the proper permissions to make state changes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2019 07:26 AM
Refer the below training doc provide by servicenow getting and setting response via Scripted Web services refer this
Mark my ANSWER as CORRECT n also HELPFUL if it works

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2019 08:09 AM
The script looks ok but since you are executing the GlideRecord update in the scripted REST block make sure that the users who is authenticating has the proper permissions to make state changes.