dynamically setting the sysid on rest end points for put method

jugnu1
Kilo Contributor

We have two instances(A nd B) and we are creating incidents in one(B) and creating incident for same on other instance(A) using a business rule and using post method

request.setEndpoint('https://instanceA-service-now.com/api/now/table/incident');

request.setHttpMethod(‘POST’);

We are sending the sys-id of incident created as well to instance A so that i can make a connection back and use it to update the record in future using the sys id with other record information

And we are able to send the data and create incident at instance(B)

Now we want to update the incident on instance(B) based on changes in instance(A)

We are using the business rule

request.setEndpoint('https://InstanceB.service-now.com/api/now/table/incident/bfbc42812f9d20107c652e5df699b63f'/'');

request.setHttpMethod('PUT');

this sysid is of the incident record i created on instance B and brought the value here to Instance A and using to make a connection back for update

But we are not able to set the dynamic value of sys-id it only works when i use hardcode value as mentioned above

Connection is there but we can’t execute using the dynamic value

1 ACCEPTED SOLUTION

Stephen Sturde
Tera Guru

Hi jugnu,

The end point is a simple string that can be constructed if you have the desired value available. What if you try appending the target sys_id to your endpoint. Something like this:

var targetID = current.other_sysid;
var ourEndpoint = 'https://InstanceB.service-now.com/api/now/table/incident/';
var ourREST = new sn_ws.RESTMessageV2();

//option 1
ourREST.setEndpoint(ourEndpoint); 
ourREST.setEndpoint(ourREST.getEndpoint() + targetID);

//option2
ourREST.setEndpoint(ourEndpoint + targetID);

 

-Stephen

View solution in original post

6 REPLIES 6

Stephen Sturde
Tera Guru

Hi jugnu,

The end point is a simple string that can be constructed if you have the desired value available. What if you try appending the target sys_id to your endpoint. Something like this:

var targetID = current.other_sysid;
var ourEndpoint = 'https://InstanceB.service-now.com/api/now/table/incident/';
var ourREST = new sn_ws.RESTMessageV2();

//option 1
ourREST.setEndpoint(ourEndpoint); 
ourREST.setEndpoint(ourREST.getEndpoint() + targetID);

//option2
ourREST.setEndpoint(ourEndpoint + targetID);

 

-Stephen

that works Stephen thanks a lot.

Hi Stephen,

I am trying this and it's not working for me.

Can you please take a look at my code below and my responses I get in my logs.

processRecentResponses: function() {
try {
var grx = new GlideRecord('u_xmatters_data');
grx.addQuery('u_responder', '');
grx.addQuery('u_received', '');
grx.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
grx.query();
while (grx.next()) {

var list = grx.u_event_id.toString(); //this is your list collector
var arrayList = list.split(","); //turn your list collector into an array


gs.info("Event ID's are " + arrayList);
}
for (var s = 0; s < arrayList.length; s++) {


var targetID = arrayList[s] + "?embed=recipients&targeted=true";
gs.info("xMatters target ID is " + targetID);
var ourEndpoint = 'https://oursite-np.na1.xmatters.com/api/xm/1/events/';
var ourREST = new sn_ws.RESTMessageV2();
gs.info("xMatters end point is " + ourREST);

var r = ourREST.setEndpoint(ourEndpoint + targetID);
// initiate REST message
// var r = new sn_ws.RESTMessageV2('xMatters', 'Get Responses');
gs.info("xMatters r is " + r.toString());
// init response variables
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

 

My Log for Event ID's is working.

My log for xMatters target ID is working

My log for xMatters end point gives me xMatters end point is [object RESTMessageV2]

My log for xMatters r is shows as undefined and my response, responseBody and httpStatus are undefined.

Any ideas?

Thanks,

Stacy

Hi Stacy,

It seems like you may have an extraneous variable in the REST portion. I don't think you need 'r'. Can you try the simplified version below and let me know how it goes?

var targetID = arrayList[s] + "?embed=recipients&targeted=true";
gs.info("xMatters target ID is " + targetID);
var ourEndpoint = 'https://oursite-np.na1.xmatters.com/api/xm/1/events/';
var ourREST = new sn_ws.RESTMessageV2();

gs.info("xMatters end point is " + ourREST);

ourREST.setEndpoint(ourEndpoint + targetID);

gs.info("xMatters ourREST is " + ourREST.toString());

// init response variables
var response = ourREST.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
-Stephen