Schedule job to call a rest Message

Vijay53
Giga Contributor

Hi

 

I have built an integration with a third pary tool.  I am able to send and retrieve the information from the tool.

 

I am building a scheduled job to retrive the tickets from the tird party tool.

Can anyone help me in finding out the no of records that are retrieved by the scheduled job and how to parse the data that is retrived an update the Request item table.

 

This is the code that is used to retrive the ticket information.

 

try {
var r = new sn_ws.RESTMessageV2('XXXXXX', 'Select');
r.setStringParameterNoEscape('ritm_number', 'RITM0190158');

 

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

 

Appreciate the help

Vijay

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Vijay,

So you must be getting some response; print that; most probably it would be json object and you need to parse that json and then update RITM table

try {
var r = new sn_ws.RESTMessageV2('XXXXXX', 'Select');
r.setStringParameterNoEscape('ritm_number', 'RITM0190158');

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

gs.info('Response is: ' + responseBody);

}
catch(ex) {
var message = ex.message;
}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Mark Roethof
Tera Patron
Tera Patron

Hi there,

If interested in the number of record, assuming this is against an other ServiceNow instance on sc_req_item, you could go for something like:

var length = JSON.parse(responseBody).result.length;
gs.info(length);

And an example for parsing:

var length = JSON.parse(responseBody).result.length;

for(var i = 0; i < length; i++) {
	var short_description = JSON.parse(responseBody).result[i].short_description;
}

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark

The issue that i am trying to address is to this.  WHen I execute this it would pull only one record.  I want to use the scheduled job to pull all the records that are updated on the third party tool.

var r =new sn_ws.RESTMessageV2('XXXXXX', 'Select');
r.setStringParameterNoEscape('ritm_number', 'RITM0190158');

Thank you

 

I don't about your 3rd party tool ofcourse, though taking ServiceNow as example, with below you could already obtain all records (added a setLimit:10).

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://some-instance.service-now.com/api/now/table/sc_req_item?sysparm_limit=10');
request.setHttpMethod('GET');

//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'admin';
var password = 'admin';

request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");

var response = request.execute();
gs.log(response.getBody());

It depends on the REST Message, Method, etc.. you are using. This example is a GET on the whole table, with some additional parks (in this case just a setlimit, though active=true, etc would also be possible).

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn