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.

How to get yesterday date

Yair2
Giga Contributor

Hi all,

I have Scheduled script which creates Event when date field maches to todays date, the event triggers notification.

So far works good, but now the demand is to fire the event 1 before the due date, what i need to change in my script to make it work ?


var currentdate = new GlideDate();
currentdate.getByFormat("dd-MM-yyyy");
gs.info(currentdate);
var gr = new GlideRecord("u_business_management_task");
gr.addEncodedQuery("u_target_dateISNOTEMPTY");
gr.query();

while(gr.next()){
if((currentdate) == (gr.u_target_date)){
//gs.info("in the if case");
gs.eventQueue('business.managment.task.due.date',gr,gr.u_assigned_to,gr.u_number);
}
}

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Have a look at this docs page:
https://docs.servicenow.com/bundle/newyork-application-development/page/app-store/dev_portal/API_ref...

It has multiple examples about .addDays(-1)

Not tested, but below might already work:

var currentdate = new GlideDateTime();
currentdate.addDays(-1);

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

Kind regards,
Mark

---

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

View solution in original post

2 REPLIES 2

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Have a look at this docs page:
https://docs.servicenow.com/bundle/newyork-application-development/page/app-store/dev_portal/API_ref...

It has multiple examples about .addDays(-1)

Not tested, but below might already work:

var currentdate = new GlideDateTime();
currentdate.addDays(-1);

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

Kind regards,
Mark

---

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

Mark Roethof
Tera Patron
Tera Patron

Other way might be to rethink your code. You could do the date check already within the .addEncodedQuery. I don't really see why you would do a GlideRecord query, going thru all results, and then checking if the date matches? Why not already in the query?

Just build the query on a List, and copy the query. If unfamiliar with this, read this article I wrote a few weeks ago.
https://community.servicenow.com/community?id=community_article&sys_id=2757d97fdb2b7f84feb1a851ca961...

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

Kind regards,
Mark

---

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