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

dvp
Mega Sage
Mega Sage

I think the other system is expecting the lastModifiedOnRange as an input parameter

Can you try passing them as below

 

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

Thanks DVP, for your help.

I tried your solution but that didn't seem to work. 

What finally worked is a solution from Ankur Bawiskar.  I passed the variables as QueryParameters with the following syntax -

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

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

Withe the above, the REST 'GET' query successfully extracts the desired set of records for any given date-time range.

 

Thank you, once again.

-Rajan