Sending a REST Message with no REST Message Record for incident records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2017 06:04 AM
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
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2017 06:25 PM
1. What is this target "http://12.12.14.25/incident/" ?
2. Do you see an inbound JSON at target?
3. Do you see a response being sent out from Target?
4. What is your ServiceNow instance version? If you are on Istanbul/Jakarta: we have something called HTTP logging:
5. What do you see in these logs?
6. Have you tried testing REST Requests to your target "http://12.12.14.25/incident/" via tools like POSTMAN? I recommend Test and debug.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2017 08:41 PM
1. What is this target "http://12.12.14.25/incident/" ? - This Http endpoint created by me which accepts the Post requests. 2. Do you see an inbound JSON at target? No sure what u are asking 3.Do you see a response being sent out from Target? whne i log the response . I am seeing {} this . 4 and 5 the point. I am will use this logging and post the results. 6. Have you tried testing REST Requests to your target "http://12.12.14.25/incident/" via tools like POSTMAN? I recommend Test and debug - I have tried posting array of json object using postman and my http endpoint is able to receive it. Vab singhal , My query is am i following the correct way to send a rest message using RestMessageV2() or any other method is available for it. Thanks Arul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2017 12:46 AM
hi,
it is working without any problem.
// now script - run in background
gr.get('d71f7935c0a8016700802b64c67c11c6');
var str = JSON.stringify(gr);
var restMessage = new sn_ws.RESTMessageV2();
restMessage.setHttpMethod("post");
restMessage.setEndpoint("http://localhost:4000/incident");
restMessage.setRequestBody(str);
var response = restMessage.execute();
gs.addInfoMessage(JSON.stringify(response.getBody()));
node - server:
//create a server object:
http.createServer(function (req, res) {
req.on('data', function(data){
var str = data.toString();
res.write('Hello World!'); //write a response to the client
res.write(str);
res.end(); //end the response
})
}).listen(4000); //the server o
But if i use the "express" server , then i am getting empty response just like you said. Hope this helps.
updated script:-
now script:-
gr.get('d71f7935c0a8016700802b64c67c11c6');
var str = JSON.stringify(gr);
var restMessage = new sn_ws.RESTMessageV2();
restMessage.setHttpMethod("post");
restMessage.setEndpoint("http://localhost:4000/incident");
restMessage.setRequestBody(str);
var response = restMessage.executeAsync();
response.waitForResponse(60);
gs.addInfoMessage(JSON.stringify(response.getBody()));
express router for "incident" path:-
var body = [];
req.on('data', function(data) {
/*console.log(JSON.stringify(data));
this is working for postman
var msg = {
msg: 'incident list recieved',
body: JSON.stringify(data),
body1: req.body
}
res.send(msg); */
body.push(data);
}).on('end', function() {
var str = JSON.parse(body.toString());
res.send({
msg: 'hello world',
body: str
});
});
Thanks
-YLN
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2017 02:19 AM
Hi Lakshmi , I have slightly modified my code . I used setRequestHeader("Content-Type","text/plain") in the script actions code and modified my node js code ( i am using express ) var app = express(); var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: false})); app.use(bodyParser.text({ type: 'text/*' })); with this modification i am getting body content as "{\"number\":\"INC0010039\",\"cmdb_ci\":\"alex-test\",\"comments\":\"\",\"desc\":\"Mobile not working due to Low battery\",\"openTime\":\"2017-09-05 08:42:17\",\"category\":\"software\"}" Appreciate your help and thanks for your response 🙂 Regards Arul