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

Niklas Peterson
Mega Sage
Mega Sage

Hi @Community Alums 

 

The error says that your endpoint does not exist. Since you are doing a put I would say that the part that does not exist is the sys_id. Meaning there is no location record in the second instance with the same sys_id as the record in the first instance.

 

Check the logic with a sys_id of a location record in the second instance.

 

Regards,
Niklas

Community Alums
Not applicable

The URL is correct, I have verified that by copying it into a secondary browser and it's fine. 
The sys_Id is identicle on both of them.  I'm using a REST Message that has the GET/POST/PUT in them and the POST is Working perfect, the GET is working perfect so something else is wrong with the PUT statement

Community Alums
Not applicable
Info MessageResponse Body: {"error":{"message":"Method not Supported","detail":"PUT method not supported for API"},"status":"failure"}

I am now getting this

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'),