The Zurich release has arrived! Interested in new features and functionalities? Click here for more

REST MESSAGE Put Help needed pelase

Community Alums
Not applicable

I have created an after update business rule that works with a PUT Rest Message endpoint.  I have tested this between two instance and two PDIs and I'm getting the same problem
2024-08-06_12-15-54.jpg

 I have verified that the SYS ID's match (as I used POST message to get this across to the other instance.  I'm kind of stuck here.  I'm trying to update the record on the second system via PUT or PATCH and nothing is workink

Here is the code for the business rule:
Table: cmn_location
Active, Advanced:
When to run: After - update - order 100
Advanced tab script:

(function executeRule(current, previous /*null when async*/) {
try { 
 var r = new sn_ws.RESTMessageV2('dev266487_contract', 'CMNLocation_put');

var body = {
	"name": current.getDisplayName('name'),
	"contact": current.getValue('contact'),
	"state": current.getDisplayValue('state'),
	"city": current.getDisplayValue('city'),
	"street": current.getDisplayValue('street')
};

var corID = current.getValue('sys_id');
r.setEndPoint('https://dev266487.service-now.com/api/now/table/cmn_location/'+corID);
r.setRequestBody(JSON.stringify(body));

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
 gs.addInfoMessage("Response Body: " + responseBody);
 gs.addInfoMessage("httpStatus: "+httpStatus);
 gs.addInfoMessage("SYSID: " + corID);
}
catch(ex) {
 var message = ex.message;
}

})(current, previous);


Does anyone see anything wrong with this.  I also have tested the REST user with Admin Rights (just to make sure ACL's are not the issue). 

Please help

1 ACCEPTED SOLUTION

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @Community Alums - Set the endpoint to `https://dev266487.service-now.com/api/now/table/cmn_location/${sys_id}` in the HTTP method, then add a variable substitution with the name sys_id.

 

(function executeRule(current, previous /*null when async*/) {
    try {
        var r = new sn_ws.RESTMessageV2('dev266487_contract', 'CMNLocation_put');
        r.setStringParameterNoEscape('sys_id', current.getUniqueValue());

        var body = {
            "name": current.getDisplayValue('name'),
            "contact": current.getValue('contact'),
            "state": current.getDisplayValue('state'),
            "city": current.getDisplayValue('city'),
            "street": current.getDisplayValue('street')
        };

        //var corID = current.getValue('sys_id');
        //r.setEndPoint('https://dev266487.service-now.com/api/now/table/cmn_location/' + corID);
        r.setRequestBody(JSON.stringify(body));

        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
        gs.addInfoMessage("Response Body: " + responseBody);
        gs.addInfoMessage("httpStatus: " + httpStatus);
    } catch (ex) {
        var message = ex.message;
    }
})(current, previous);

 

Also, there is an error in your JSON (fixed above):

 

"name": current.getDisplayName('name'),

 

View solution in original post

6 REPLIES 6

Community Alums
Not applicable

Thank you x 10000000!  I really appreciate the guidance on this.  Awesome!

BillMartin
Mega Sage

@Community Alums ,

 

Have a look at my clip, it will show you how the REST-API works.