Вы находитесь на странице: 1из 5

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

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:
Outbound web services 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.

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

hi,
it is working without any problem.

// now script - run in background

var gr = new GlideRecord('incident');

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:

var http = require('http');

//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:-

var gr = new GlideRecord('incident');

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:-

router.post('/', function(req, res, next) {

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

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

cool,

Please Hit ✅Correct, ⭐️Helpful, or ❣️Like depending on the impact of the response

Thanks
- YLN

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
is it possible to get the current event records ( incident information) . How can we get the details ,
through which object ?

This document was generated from the following discussion: Sending a REST Message with no
REST Message Record for incident records

Вам также может понравиться