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.

Business Rule Checking For Future Date

jmiskey
Kilo Sage

On our sys_user table, we have a few custom fields:

u_hire_date - this is the user's last hire date

u_new_hire_ticket_generated - this is a checkbox that indicates whether a new hire ticket has been generated

So, there is a scheduled job that runs every 10 minutes that looks to see if there are any new hires (hire date is less than 7 days in the future) who have not had a new hire ticket generated (it then generates the ticket and checks the box).

The issue that we have is when he import our user records daily, sometimes a person is rehired, in which case their u_hire_date field is updated to some future date.   So I am trying to write an After Update Business Rule on the sys_user table that looks for two things:

- the u_new_hire_ticket_generated is checked

- the u_hire_date field is more than 7 days in the future

If both of those conditions are true, it should clear the check out of the u_new_hire_ticket_generated checkbox, so when the u_hire_date field is exactly 7 days in the future, our scheduled job will generated a new hire ticket for them.   However, I am unable to get my script in my Business Rule to work (I actually wouldn't need a script if the Filter Conditions on the When to Run tab had an "more than 7 days in the future" options, but alas, it doesn't).

Here is my script attempt:

(function executeRule(current, previous /*null when async*/) {

  // Add your code here

  var chkDate = new Date();

  chkDate.setDate(chkDate() + 7);   /*days*/

  if ((current.u_new_hire_ticket_generated=='TRUE') && (current.u_hire_date > chkDate)) {

  current.u_new_hire_ticket_generated='FALSE';

  }

})(current, previous);

Can anyone see where I went wrong?

1 ACCEPTED SOLUTION

Oops.. fat finger in my code:


Where you had this:


  var futureDate = new GlideDate();


  gs.addInfoMessage("Before calc: " + futureDate);


  futureDate = futureDate.addDaysLocalTime(7);


  gs.addInfoMessage("After calc: " + futureDate);



Change to:


var futureDate = new GlideDate();


  gs.addInfoMessage("Before calc: " + futureDate);


  futureDate.addDaysLocalTime(7);


  gs.addInfoMessage("After calc: " + futureDate);


View solution in original post

11 REPLIES 11

Thank you!   Thank you!


That fixed it!


Glad I could help!