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.

Generating Event for requests reaching due date in 7 days

Learn SN
Tera Contributor

Hi Experts,

 

I want to generate an event when due date of any request ( sc_request ) is due in next 7 dyas. The type of Due Date is Date not DateTime. I have already tried addDays and it is not working.

Can someone help ?

 

Thanks!

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi,

Try this code and let me know the output.

var gdt = new GlideDateTime();
gdt.addDaysUTC(7);
var gr = new GlideRecord("sc_request");
gr.query();
while(gr.next()) {
gs.print("U date is "+ gr.getValue("u_due_date")+"---"+new GlideDateTime(gr.getValue("u_due_date")).getDate().toString());
  if(new GlideDateTime(gr.getValue("u_due_date")).getDate().toString().equals(gdt.getDate().toString())) {
    //trigger event here.
    gs.print("Reques due"+gr.number+"--"+new GlideDateTime(gr.due_date).getDate());
  } 
}

View solution in original post

3 REPLIES 3

Uncle Rob
Kilo Patron

1.  Make sure you have an event defined in the Event Registry

2.  Make sure you have a query that captures "sc_requests due in the next 7 days"

3.  Create a Scheduled Script

4.  Use your query as a the basis for a gliderecord lookup.

5.  Loop through your query results and fire the gs.eventQueue('event_name', record, parm1,parm2)

 

Since I hate working with dates in queries, I just went to a list view to create the query I wanted:
find_real_file.png

Then I copied the query from the list view breadcrumbs.

 

From there I used THE BEST SERVICENOW DEVELOPER TOOL ON THE PLANETXPLORE to built a script that I know works.

find_real_file.png

 

Two outputs, just as I expected.  Time to uncomment the gs.eventQueue line, put in the real event name and whatever I like for parameters, then copy that and toss it into a Scheduled Script.

asifnoor
Kilo Patron

Hi,

Try this code and let me know the output.

var gdt = new GlideDateTime();
gdt.addDaysUTC(7);
var gr = new GlideRecord("sc_request");
gr.query();
while(gr.next()) {
gs.print("U date is "+ gr.getValue("u_due_date")+"---"+new GlideDateTime(gr.getValue("u_due_date")).getDate().toString());
  if(new GlideDateTime(gr.getValue("u_due_date")).getDate().toString().equals(gdt.getDate().toString())) {
    //trigger event here.
    gs.print("Reques due"+gr.number+"--"+new GlideDateTime(gr.due_date).getDate());
  } 
}

Hi,

Try this code

var gdt = new GlideDateTime();
gdt.addDaysUTC(7);
var gr = new GlideRecord("sc_request");
gr.addNotNullQuery('u_due_date');
gr.query();
while(gr.next()) {
//gs.print("U date is "+ gr.getValue("u_due_date")+"---"+new GlideDateTime(gr.getValue("u_due_date")).getDate().toString());
  if(new GlideDateTime(gr.getValue("u_due_date")).getDate().toString().equals(gdt.getDate().toString())) {
    //trigger event here.
    gs.print("Reques due"+gr.number+"--"+new GlideDateTime(gr.due_date).getDate());
  } 
}

Mark the comment as a correct answer and helpful once worked.