setRequestBody not POST unless manually defined

motto
Kilo Expert

See the following - will provide an idea as to what I am trying to accomplish

(function () {
try
{
 var shortDescription = current.short_description;
 var description = current.description;
 var callerId = current.caller_id;
 var incidentNumber = current.number;
 var sysId = current.sys_id;
 var comments = current.comments;
 var details = JSON.stringify(shortDescription,sysId,description);
 var request = new sn_ws.RESTMessageV2('Ottos test instance', 'Otto_Post');
	 request.setRequestHeader("Accept","application/json");
	 request.setRequestHeader('Content-Type','application/json');request.setRequestBody("{\"short_description\":\"" + shortDescription +"}");  // THIS DOES NOT WORK - UNSURE HOW TO CALL VARIABLES ABOVE
// request.setRequestHeader('Content-Type','application/json');request.setRequestBody("{\"short_description\":\"manually created string content\"}");  THIS WORKS BUT VALUE IS MANUALLY DEFINED
 var response = request.execute();
 var responseBody = response.getBody();
 var json = responseBody;
 var obj = JSON.parse(json);            //define JSON parsing for the response JSON file to decode
 var foundSysID = obj.result.sys_id;    //decoded HEB newly created incident sys_id
 var foundNumber = obj.result.number;
 current.correlation_id = foundSysID;   //copied the sys_id incident to correlation id
 current.u_customer_ticket = foundNumber;
 current.u_escalated=1;
 current.update();                      //update the current incident
 
 gs.addInfoMessage("This incident has been escalated to HEB :  "+foundNumber); //log ticket number
 var httpStatus = response.getStatusCode();	
}
catch(ex) {
 var message = ex.message;
}
})();

1) Works if string values are manually defined

2) trying to pull string values from variables as defined (e.g. var shortDescription = current.short_description;)

Unclear what I am doing wrong to get strings from variables into request body

Thanks

Mike

1 ACCEPTED SOLUTION

motto
Kilo Expert

Fixed - here is the code (special thanks to @Javier in the SNOW Slack channel for support)

The use case here is to create a UI Action button that allows me to escalate an incident to another ServiceNow instance selectively. We didn't a business rule in this case - we want to selectively escalate incidents on a per incident basis. This method also allows for conditions to be applied to the UI Action that, if we want to, we can have the button only appear under certain circumstances. 

I was directed to create a proper variable for the body of the POST, and directed how to properly stringify the request. You can literally copy this code, substitute your own REST call, and provided all the field values match, it should largely due the trick. 

There are probably lots of ways to do this - that said, this one works!

(function () {
    try {
        var body = {
            short_description: current.getValue('short_description'),
            description: current.getValue('description'),
            caller_id: current.getValue('caller_id'),
            incident_number: current.getValue('number'),
           	correlation_id: current.getValue('number'),
			comments: current.comments.getJournalEntry(-1),
        };
        var tosend = JSON.stringify(body);
        var request = new sn_ws.RESTMessageV2('Ottos test instance', 'Otto_Post');
        request.setRequestHeader("Accept", "application/json");
        request.setRequestHeader('Content-Type', 'application/json');
        request.setRequestBody(tosend);
        var response = request.execute();
        var responseBody = response.getBody();
        var json = responseBody;
        var obj = JSON.parse(json); //define JSON parsing for the response JSON file to decode
        var foundSysID = obj.result.sys_id; //decoded newly created incident sys_id on opposing instance
        var foundNumber = obj.result.number;
			current.correlation_id = foundSysID; //copied the sys_id incident to correlation id
			current.u_customer_ticket = foundNumber;
			current.u_escalated = 1;
			current.update(); //update the current incident
        gs.addInfoMessage("This incident has been escalated to HEB :  " + foundNumber); //log ticket number
		gs.log (tosend);
		gs.log (foundnumber);
        var httpStatus = response.getStatusCode();
    } catch (ex) {
        var message = ex.message;
   }
})();

View solution in original post

2 REPLIES 2

motto
Kilo Expert

Fixed - here is the code (special thanks to @Javier in the SNOW Slack channel for support)

The use case here is to create a UI Action button that allows me to escalate an incident to another ServiceNow instance selectively. We didn't a business rule in this case - we want to selectively escalate incidents on a per incident basis. This method also allows for conditions to be applied to the UI Action that, if we want to, we can have the button only appear under certain circumstances. 

I was directed to create a proper variable for the body of the POST, and directed how to properly stringify the request. You can literally copy this code, substitute your own REST call, and provided all the field values match, it should largely due the trick. 

There are probably lots of ways to do this - that said, this one works!

(function () {
    try {
        var body = {
            short_description: current.getValue('short_description'),
            description: current.getValue('description'),
            caller_id: current.getValue('caller_id'),
            incident_number: current.getValue('number'),
           	correlation_id: current.getValue('number'),
			comments: current.comments.getJournalEntry(-1),
        };
        var tosend = JSON.stringify(body);
        var request = new sn_ws.RESTMessageV2('Ottos test instance', 'Otto_Post');
        request.setRequestHeader("Accept", "application/json");
        request.setRequestHeader('Content-Type', 'application/json');
        request.setRequestBody(tosend);
        var response = request.execute();
        var responseBody = response.getBody();
        var json = responseBody;
        var obj = JSON.parse(json); //define JSON parsing for the response JSON file to decode
        var foundSysID = obj.result.sys_id; //decoded newly created incident sys_id on opposing instance
        var foundNumber = obj.result.number;
			current.correlation_id = foundSysID; //copied the sys_id incident to correlation id
			current.u_customer_ticket = foundNumber;
			current.u_escalated = 1;
			current.update(); //update the current incident
        gs.addInfoMessage("This incident has been escalated to HEB :  " + foundNumber); //log ticket number
		gs.log (tosend);
		gs.log (foundnumber);
        var httpStatus = response.getStatusCode();
    } catch (ex) {
        var message = ex.message;
   }
})();

Hi Motto,

Could you please help me to understand from where do we get obj.result?

 

find_real_file.png

I am trying to display correlation_id. could you please help to understand.

Thanks and Regards,

Meenal