How do I specify Date-Time range in REST 'GET' request?

RajanMurkute
Mega Guru

I am trying to integrate our Incident Management system with another application used by a vendor. My task is to pull service-tickets created in vendor's application, based on date-time range (start, end); and  create corresponding records in our Incident table. For this I have created a scheduled Job that runs periodically. The scheduled job uses REST 'GET' request.

My code is as follows -

//Initiate new web-service GET request
   var request = new sn_ws.RESTMessageV2();
   request.setEndpoint('https://something.com/rest/Ticket');
   request.setHttpMethod('GET');

//Set authentication user/password
  var user = 'xyz';
  var password = 'abcd';
  request.setBasicAuth(user,password);

//Set request Headers
  request.setRequestHeader('Content-Type','application/json');
  request.setRequestHeader("Accept","application/json");
  request.setRequestHeader('Cache-Control','no-cache');

//Set request Parameters
  request.includeTicketsWithoutDevices = true;
  request.lastModifiedOnRange("(start, 2019-12-16T00:00:00Z)(end, 2019-12-16T23:59:59Z)");

//Execute the request
var response = request.execute();

//

NOTE: 'includeTicketsWithoutDevices' and 'lastModifiedOnRange' are valid properties in the vendor's application API.

When the scheduled job runs, it returns response with 'httpStatus=200' and  returns 2 records in JSON format, as shown below -

//Response Body
{
"items" : [ {
"id" : "SOC9229",
"customerName" : "ACME CORP",
"partnerId" : "PID704571",
"partnerName" : "EMCA Outsourcing",
"lastModifiedOn" : "2019-11-23T05:50:13.000Z",
"issueDescription" : "TEST Issue Description",
"priority" : "LOW",
"ticketStatus" : "Assigned",

},
{
"id" : "SOC8982",
"customerName" : "ACME CORP",
"partnerId" : "PID704571",
"partnerName" : "EMCA Outsourcing",
"lastModifiedOn" : "2019-12-03T02:20:55.000Z",
"issueDescription" : "TEST Issue Description",
"priority" : "LOW",
"ticketStatus" : "Assigned",
} ],
"start" : 0,
"limit" : 100,
"totalCount" : 2

The problem is, no matter what date-time range I specify, it always pulls the same two records. In those records, too, you may notice that 'lastModifiedOn' values does not correspond to  request.lastModifiedOnRange("(start, 2019-12-16T00:00:00Z)(end, 2019-12-16T23:59:59Z)");

However, the same GET request works fine in POSTMAN, where I specify 'lastModifiedOnRange' under Parameters. (see the screen shot in attachments). It fetches the correct set of records that belong to the date-time range I provide.

Is there something wrong with my statement -

    request.lastModifiedOnRange("(start, 2019-12-16T00:00:00Z)(end, 2019-12-16T23:59:59Z)");  ?

Is there another way of specifying the date-time range in 'GET' request?

Please help.

1 ACCEPTED SOLUTION

Hi,

I think you will have to include those in query parameters 

can you try this as below and check once

request.setQueryParameter("includeTicketsWithoutDevices", "true");

request.setQueryParameter("lastModifiedOnRange", "(start, 2019-12-16T00:00:00Z)(end, 2019-12-16T23:59:59Z)");

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

View solution in original post

11 REPLIES 11

Hi,

how many records you get from postman when you give that date range?

it seems some issue around how the values are sent

//Set request Parameters
request.includeTicketsWithoutDevices = true;
request.lastModifiedOnRange("(start, 2019-12-16T00:00:00Z)(end, 2019-12-16T23:59:59Z)");

how are you sending it from postman? are those values being sent in the request body or headers?

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

//

1) includeTicketsWithoutDevices = true ;
2) lastModifiedOnRange("(start, 2019-12-16T00:00:00Z)(end, 2019-12-16T23:59:59Z)") ;

The above two properties are sent as parameters in POSTMAN. (See the screen-shot.) There is no Body in the GET request

find_real_file.pngIn POSTMAN it fetches 1, records which is correct.

Response:

{
"items" : [ {
"id" : "SOC9238",
"customerName" : "ACME CORP",
"partnerId" : "PID704571",
"partnerName" : "EMCA Outsourcing",
"lastModifiedOn" : "2019-12-18T05:50:13.000Z",
"issueDescription" : "TEST Issue Description",
"priority" : "LOW",
"ticketStatus" : "Assigned"
}]

"start": 0,
 "limit": 100,
 "totalCount": 1
}
 
I have tried several ways to get this GET working using  ServiceNow scripted API, but with no success.
 
Instead of using - request.lastModifiedOnRange("(start, 2019-12-18T00:00:00Z)(end, 2019-12-18T23:59:59Z)") ; 
I tried using a different parameter (id) - request.id("SOC9238"); . It still gives wrong results.  Whereas, in POSTMAN everything works fine.
 

Hi,

I think you will have to include those in query parameters 

can you try this as below and check once

request.setQueryParameter("includeTicketsWithoutDevices", "true");

request.setQueryParameter("lastModifiedOnRange", "(start, 2019-12-16T00:00:00Z)(end, 2019-12-16T23:59:59Z)");

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

Hi Ankur,

 

That worked!!!

As you suggested I used the syntax -

request.setQueryParameter("includeTicketsWithoutDevices", "true");

request.setQueryParameter("lastModifiedOnRange", "(start, 2019-12-16T00:00:00Z)(end, 2019-12-16T23:59:59Z)");

It extracted the desired number of records. I tested with different date-time range and everything works well now.

Thanks a million, for helping resolve this issue that has was troubling me for a long.

-Rajan

 

 

Glad that it worked.

Regards

Ankur

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