Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Issue querying records between two dates

brianschwieder
Tera Contributor

I have written a script that I want to trigger an event when an incident is between 5 and 6 days old.

var sixdays = gs.daysAgo(-6);

  var fivedays = gs.daysAgo(-5);

  var target = new GlideRecord('incident');

    target.addQuery('subcategory', "QNetwork");

    target.addQuery('opened_at', '>=', sixdays);

    target.addQuery('opened_at', '<=', fivedays);

    target.addQuery('active', true);

    target.query();

  while (target.next()) {

  gs.eventQueue("inl.qnet.inc.fivedaysold",target, gs.getUserID(), gs.userName());

  }

If I comment out the five day query, I get back results for all tickets opened in the last six days. However with the five day query added back in I get no results when I should get back three.

What am I missing here?

1 ACCEPTED SOLUTION

Mike Allen
Mega Sage

var sixdays = gs.daysAgo(6);


var fivedays = gs.daysAgo(5);


var target = new GlideRecord('incident');


target.addQuery('opened_at', '>=', sixdays);


target.addQuery('opened_at', '<=', fivedays);


target.query();


while (target.next()) {



  gs.print(target.number);



}



I did that and got records.   I used 6 and 5, not -6 and -5.



I think negative with daysAgo is in the future.


View solution in original post

3 REPLIES 3

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Brian,



I think you are not able to fetch the records as your and condition is not satisfied. You can check the same on the list view filter once. I think you have to change this to or condition.


Mike Allen
Mega Sage

var sixdays = gs.daysAgo(6);


var fivedays = gs.daysAgo(5);


var target = new GlideRecord('incident');


target.addQuery('opened_at', '>=', sixdays);


target.addQuery('opened_at', '<=', fivedays);


target.query();


while (target.next()) {



  gs.print(target.number);



}



I did that and got records.   I used 6 and 5, not -6 and -5.



I think negative with daysAgo is in the future.


Thanks!


I knew it was something small I missed but just couldn't see.