Proper way to update state from API

mdjoseph12
Giga Contributor

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

 

1 ACCEPTED SOLUTION

DScroggins
Kilo Sage

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.

View solution in original post

2 REPLIES 2

Ct111
Giga Sage

Refer the below training doc provide by servicenow getting and setting response via Scripted Web services refer this

https://developer.servicenow.com/app.do#!/training/article/app_store_learnv2_rest_madrid_scripted_re...

 

Mark my ANSWER as CORRECT n also HELPFUL if it works

DScroggins
Kilo Sage

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.