- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2017 08:40 AM
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?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2017 09:23 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2017 12:23 PM
One thing I discovered was I needed to make this "Before Update", not "After Update". So I changed that,
I also noticed that dates in our dateDiff calculation were reversed. I switched it, and not it is working, though not quite right. It appears that it is not adding the 7 days properly. I am returning the dateDiff calculation in and Info Message to see what it is at the time.
So, here is my current version of the code:
(function executeRule(current, previous /*null when async*/) {
var futureDate = new GlideDate();
futureDate = futureDate.addDaysLocalTime(7);
var dateDif = gs.dateDiff(futureDate.getDisplayValue(),current.u_hire_date.getDisplayValue(), true);
gs.addInfoMessage(gs.dateDiff(futureDate.getDisplayValue(),current.u_hire_date.getDisplayValue(), true));
if (current.u_new_hire_ticket_generated==true && dateDif > 0) {
current.u_new_hire_ticket_generated=false;
}
})(current, previous);
So, because we are adding 7 days to the current date, the dateDiff calculation should be greater than zero when the date is after April 12, 2017 (and less than zero if before that date). But in my testing, it looks like it is comparing it to now, not one week from now.
I entered April 5, 2017 (today), and my Info Message returned -54743
I then entered April 6, 2017 (tomorrow), and my Info Message returned 31621
So it appears that the adding in of 7 days is not working properly. Can you see why?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2017 01:11 PM
OK, I think it is choking on the adding 7 days calculation.
I did some testing, returning the value of futureDate before and after the calculation of adding 7 days like this:
var futureDate = new GlideDate();
gs.addInfoMessage("Before calc: " + futureDate);
futureDate = futureDate.addDaysLocalTime(7);
gs.addInfoMessage("After calc: " + futureDate);
My messages returned:
Before calc: 2017-04-05
After calc: undefined
The question is, why is it doing this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2017 01:23 PM
Ah!
I was just going to suggest trying .addDays(7) instead of .addDaysLocalTime()
That method is a new one, so if you're on a pre-Helsinki(?) system, it won't exist.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2017 08:53 AM
I tried that, and it still returns that "undefined" error.
We are using Helsinki.
Really odd...
Any idea what else could be causing this error?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2017 09:23 AM
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);