Sending a REST Message with no REST Message Record for incident records

arulvelu
Kilo Contributor

Hi Folks , I   have written this script to check for active incidents that had happened few minutes ago and send this details to 3rd party rest api (   node server   written by me) .

sendNewIncidents();

function sendNewIncidents() {

  var d = new Date();

  var gr = new GlideRecord('incident');

  gr.addQuery('priority',1);

  gr.addQuery('impact',1);

  gr.addQuery('active',true);

  gr.addQuery('sys_created_on','>=', gs.minutesAgo(560));

      gr.query();

  var incident = {};

      var incidentList = [];

  while (gr.next()) {

  incident = {};

      gs.addInfoMessage(gr.number);

  gs.addInfoMessage(gr.short_description);

  gs.addInfoMessage(gr.category);

  gs.addInfoMessage(gr.cmdb_ci);

  incident.number =   gr.number + '';

  incident.cmdb_ci = gr.cmdb_ci.value + '';

  incident.desc = gr.short_description +'';

      incident.openTime = gr.opened_at + '';

  incident.category = gr.category+ '';

  incidentList.push(incident);

               

  }

      gs.log("Lenght is:"+incidentList.length);

      for(i=0;i<incidentList.length;i++) {

  var incidentDetails = incidentList[i];

  gs.log (JSON.stringify(incidentDetails));

      var dataSend =   JSON.stringify(incidentDetails);

  var stringData= new global.JSON().encode(dataSend);

  gs.log(stringData);

  var restMessage =   new sn_ws.RESTMessageV2();

      restMessage.setHttpMethod("post");

      restMessage.setEndpoint("http://12.12.14.25/incident/");

      restMessage.setRequestBody(stringData);

      var response = restMessage.executeAsync();

  gs.log(JSON.stringify(response));

  }

   

   

}

I am creating a restmessage with no message record in the forloop and posting it to my http endpoint "http://12.12.14.25/incident/" and doing this in async way. When i execute this script, i am debugging the response in node server where i get only { } . This as result. Please let me know where i have gone wrong. Thanks Arul

Message was edited by: Arulvelu Gunasekaran

Message was edited by: Arulvelu Gunasekaran

12 REPLIES 12

lakshmi , what is the difference between buiness rule and script actions. If i need to test this script ( currently i have tested this script in script debugger) How can i make it live in script actions.


Hi ,


  That depends on when you want to send this data node server.   if you want send when record changes , then BR is good.



Please find the docs here.



Business Rules - ServiceNow Wiki


http://wiki.servicenow.com/index.php?title=Script_Actions#gsc.tab=0




Thanks


- YLN


Hi Lakshmi , sorry for bugging you.. I have a question to clarify. I want my node server to subscribe incident.inserted events in my node server . Is there any option that service now provides to send events via webhook. Regards Arul


So whenever you inserted a new incident record, that information (incident record ) should pass to your node server. if that is the requirement,   you can write a Business rule on incident table and   call   RESTMessageV2.



Thanks


- YLN