
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2018 02:18 PM
I have two date fields on a scoped app, those are: u_received_date , u_dob
Subtracting these values will tell me the age of the user at the time the request was received.
I have created the following Business Rule but it doesn't calculate the age correctly.
For example,
If the received date = 07/10/18 and the date of birth = 08/18/1979 the value being populated is 39 and it should be 38. Please see my code below any help will be appreciated.
(function executeRule(current, previous /*null when async*/) {
var recDa = new GlideDateTime(current.u_received_date.getDisplayValue());
var recDaYear = recDa.getYearLocalTime();
var bday = new GlideDateTime(current.u_dob.getDisplayValue());
bdayYear = bday.getYearLocalTime();
var age = recDaYear - bdayYear;
//gs.addInfoMessage(age);
current.u_age = age;
})(current, previous);
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2018 11:18 PM
Hi,
Please mark this as "Correct Answer" if I have given you a sufficient answer and you have no other questions.
Best Regards,
Daniele

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2018 08:55 AM
Unfortunately I can not use dateDiff in a scoped app 😞

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2018 09:47 AM
Thank you everyone - I was able to get the following to work using Scoped app:
(function executeRule(current, previous /*null when async*/) {
var gdt1 = new GlideDateTime(current.u_received_date.getDisplayValue());
var gdt2 = new GlideDateTime(current.u_dob.getDisplayValue());
duration = GlideDateTime.subtract( gdt2, gdt1 ).getDayPart();
duration = parseInt( duration / 365 ); // Years
//gs.addInfoMessage(duration);
current.u_age = duration;
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2018 11:18 PM
Hi,
Please mark this as "Correct Answer" if I have given you a sufficient answer and you have no other questions.
Best Regards,
Daniele

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2018 09:54 AM
FYI - I also did the following:
current.u_age = duration.toPrecision(2); //added to avoid the decimal point