The CreatorCon Call for Content is officially open! Get started here.

Run REST (POST & GET) call on one Business rule

kevinray
Giga Expert

Hello fellow developers.

I'm fairly new and dont have much REST or Web Services experience. But i think after all the hours I spent i'm very close so i just need a little bit of help.

------------BACKGROUND-----------------

We are integrating with Foglight. Foglight uses Rest to create a ServiceNow Incident. They populate two additional fields in the Incident

u_reference : which contains an 'alarmID'

u_foglight_alert_ server: which contains the target server of the alarm.

They way their API works, you have to run a POST login command to get a 'Auth-Token' using basic authentication. Then you have to take that token you get, and pass it in a "Get" (along with the alarmID and alert server) to clear the alarm. This Token only lasts about 10 mins, so you have to generate a new token before every time you want to run a "GET"...(seems really dumb way of doing things to me, but that's how they say I have to do it)

I created both end points and have tested successfully using the "test" ink and the "Value Substitutions" list.

I run the post, and get the 'token' back.

I then take that token, put is as a variable substitution in the GET endpoint, and get a successful message back.

All is confirmed as working

-------MY SOLUTION-------------------

I needed to trigger this 'clear alarm' rest call whenever these incidents were put in a resolved state..To accomplish this, I created a business rule that triggers when these particular incidents are moved to resolve state. Then got the code for the "POST" and "GET using the "preview Script Usage" link on each rest call respectively to put in to my business rule.

  • I then put POST message first (passing the foglight_alert_server) to log in and get a token
  • I put a "JSON.parse(responceBody) to get the 'token' value out of the JSON string and assigned that a variable
  • I then put the GET message after that and set the alarmID, serverhost, and token that i got from the POST

The response of the raw post rest message looks something like this:

{"status":1,"data":{"user":{"id":"8e94015b-8ec0-46d6-aff3-c098dbfd77f5","lastLogonTS":1504196220230,"name":"foglight_srvnow","groups":["Foglight Operators","Foglight Users","Foglight Administrators","Cartridge Developers"],"roles":["Operator","Dashboard User","VMware QuickView User","Support","VMware Automation User","General Access","Core Reports","Advanced Operator","Console User","Dashboard Designer","VMware Administrator","Cartridge Developer","Command Line Access","VMware Report User","Anybody","Capacity Management Administrator","Chargeback Administrator","Administrator","API Access","Report Manager"]},"token":"jjhju8e0sfjv9basa7rkknpqlt"}}

---------ISSUE --------------

When I put the incident in "resolved" state, i know the BR is running because my note (that is the last commend in the BR) is added to the worknotes that shows "Sent clear request to Foglight for server: '+ serverName1 + ' & Alarm ID: ' + alarm;" where serverName1 and alarm are populated with those fields form the incident.

We are however seeing a 401 error on the Foglight system for the GET which is an authentication issue. This leads me to believe that the token isn't getting passed.

---------MY QUESTION---------------

Is it possible to pass the variable "authCode" that i created from the POST, in to the setString Parameter of the GET as I am attempting to do or should I be doing this differently?

Here is my business rule

try {

var serverName = current.u_foglight_alert_server;

var r = new sn_ws.RESTMessageV2('Foglight Integration', 'post');

r.setStringParameter('serverhost', serverName);

var response = r.execute();

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

var getAuth = JSON.parse(responseBody);

var authCode = getAuth.token;

}

catch(ex) {

var message = ex.getMessage();

}

try {

var r = new sn_ws.RESTMessageV2('Foglight Integration', 'get');

var alarm = current.u_reference;

var serverName1 = current.u_foglight_alert_server;

r.setStringParameter('alarmID', alarm);

r.setStringParameter('serverhost', serverName1);

r.setStringParameter('token', authCode);

var response = r.execute();

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

}

catch(ex) {

var message = ex.getMessage();

}

current.work_notes ='Sent clear request to Foglight for server: '+ serverName1 + ' & Alarm ID: ' + alarm;

current.update();

1 ACCEPTED SOLUTION

kevinray
Giga Expert

Ok. I figured it out. My code works perfectly....the issue is that the JSON data was structure such that it was an object inside an object so when i would try to parse, it couldn't find it. It wasn't until i put my JSON in to a JSON "Format" that i realized the element i was looking for was inside another element, so I had to change my line 11 to be:



var authCode = getAuth.data.token;  



Once i did that, it found it just fine, and passed it to the next rest call just fine.



Thanks Michael for pointing out the obvious that i totally missed and that was to make sure the value was even being captured. Troubleshooting 101. duh...



Cheers.


View solution in original post

7 REPLIES 7

ayush_saxena
Tera Expert

Hello Kevin,

 

In foglight, how did you consume the Servicenow API ?

Hello Ayush,

I'm not a FogLight admin or developer so I unfortunately can not answer your question. I built this integration in my previous company, and worked closely with the Foglight administrator...that person set up Foglight.

I would suggest you contact your FogLight support team to get that information. I do recall the FogLight admin having a bit of trouble setting it up, but once he reached out to the FogLight account manager/support team for help, they were able to get it set up.

Servicenow10
Kilo Guru

hi developers,

 

As am not a pro in service-now need help I have variables section on my task form or am using third party application......so when am fetching the details using the application in my response am not able to get the values of the  variable field and for that i wrote a business rule  

 

try {
var r = new sn_ws.RESTMessageV2('HTTP_REST_API', 'POST');
r.setStringParameterNoEscape('sys_id',current.sys_id);
r.setStringParameterNoEscape('description',current.description);
r.setStringParameterNoEscape('short_description',current.short_description);
r.setStringParameterNoEscape('instance_id',current.variables.instance_id); // variable field 

 

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.getMessage();
}

 

as you can see in the above code the instance field value is needed but am not getting where am going wrong...please help me with this

 

thanks in advance!!