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

Valor1
Giga Guru

You're mixing and matching date objects.


On line 4, you're using a JS date, but SN doesn't use/convert it's date/date-time fields to JS Date objects.



Changing your comparison to gs.dateDiff() should work for you:


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


  var futureDate = new GlideDateTime(); // or GlideDate, depending on what field type "u_hire_date" is


  futureDate.addDaysLocalTime(7);


  var dateDiffCalc = gs.dateDiff(current.u_hire_date.getDisplayValue(), futureDate.getDisplayValue(), true);



  if (current.u_new_hire_ticket_generated == true && dateDiffCalc > 0) {


          current.u_new_hire_ticket_generated = false;


  }


})(current, previous);



gs.dateDiff documentation here: https://developer.servicenow.com/app.do#!/api_doc?v=helsinki&id=r_GS-dateDiff_S_S_B


https://developer.servicenow.com/app.do#!/api_doc?v=helsinki&id=r_GS-dateDiff_S_S_B



[EDIT] Updated fat finger in my code


Believe it or not, I tried something similar to that first that didn't work.   I used GlideDateTime and addDaysLocal, but it didn't work.   I did not use DateDiff though.



So, I checked, and u_hire_date is just a Date, so I used your GlideDate option.   However, when I tested it out, it did not seem to work.   I am not sure why it isn't working.


I have seen funkiness as it applies to checkboxes and whether or not TRUE/FALSE should be enclosed in single quotes or not, so I tried both ways.


But, still no luck.



Any other ideas?


Ah yes, I missed that.


When you set checkbox values, set them like this (and case matters, too):


current.u_new_hire_ticket_generated = false;



The same goes for checking your condition:


if (current.u_new_hire_ticket_generated == true && dateDiff > 0)


Unfortunately, it still doesn't seem to be working.



Here is the latest version of the code, with the changes we discussed:



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


  var futureDate = new GlideDate();


  futureDate = futureDate.addDaysLocalTime(7);


  var dateDiff = gs.dateDiff(current.u_hire_date.getDisplayValue(), futureDate.getDisplayValue(), true);



  if (current.u_new_hire_ticket_generated==true && dateDiff > 0) {


          current.u_new_hire_ticket_generated=false;


  }


})(current, previous);