- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2021 02:26 AM
Hi all,
I have a requirement of calculating the age in years and months based on a date of birth field:
Age is a single line text variable and is read only. It should populate age based on date of birth in a format like : example : 5 years 4 months.
I am not knowledgable to glide date functions. Can someone help me with a script for this?
Thanks in advance!
Solved! Go to Solution.
- Labels:
-
Script Debugger
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2021 10:56 PM
Hi,
I don't find issue in my instance. Please share me the date you are passing and the output you get along with the alert details.
I removed some unwanted text in the code. You can use this:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setVisible('certification_one',false);
g_form.hideFieldMsg('claim_dependency',true);
g_form.hideFieldMsg('insurance_plan',true);
}
var ageArr = newValue.split(" year");
var ageYear = 0;
if(ageArr.length > 1){
ageYear = parseInt(ageArr[0]);
alert('1 '+ageYear);
}
if(newValue != "21 years" && ageYear >= 21){
alert(ageYear);
g_form.setVisible('certification_one',true);
g_form.setMandatory('certification_one',true);
g_form.showFieldMsg('claim_dependency','Dependency ends at the 21st birthday unless your child is eligible for special allowance. See more information here.','info',true);
g_form.showFieldMsg('insurance_plan','A child can be enrolled in insurance up until the calendar year in which they turn 25 unless employed or married. Please confirm below.','info',true);
}
else{
g_form.setMandatory('certification_one',false);
g_form.setVisible('certification_one',false);
g_form.hideFieldMsg('claim_dependency',true);
g_form.hideFieldMsg('insurance_plan',true);
}
}
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2021 12:01 AM
Tried with 10-10-2000, worked correctly
Entered 16-08-2000, worked correctly
Entered 15-02-2000, worked correctly
Entered 27-07-2000, worked correctly
Entered 26-07-2000
Seems to work correctly now! updated the script with the unwanted text removed , maybe that was causing the issue. Thanks!
But the field message still disappears when the age is more than 21 years and if I change the value in the dropdown. Can you find a fix for that as well? Do I need to write another onchange script for this field? If yes, what would be that. Kindly help.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2021 12:56 AM
Hi Anshi,
Thats the default behaviour. The message will be cleared automatically if the value changes. There is no option available to make it appear always.
The work around is to create on change client script for each variable with this issue to show the message. You can create onchange catalog client script for that variable with the below script:
Note: Replace age in g_form.getValue("age") line with correct variable name
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setVisible('certification_one',false);
g_form.hideFieldMsg('claim_dependency',true);
g_form.hideFieldMsg('insurance_plan',true);
}
var ageValue = g_form.getValue("age");
// Update age with the actual varible name that store age
var ageArr = ageValue.split(" year");
var ageYear = 0;
if(ageArr.length > 1){
ageYear = parseInt(ageArr[0]);
}
if(ageValue != "21 years" && ageYear >= 21){
g_form.setVisible('certification_one',true);
g_form.setMandatory('certification_one',true);
g_form.hideFieldMsg('claim_dependency',true);
g_form.hideFieldMsg('insurance_plan',true);
g_form.showFieldMsg('claim_dependency','Dependency ends at the 21st birthday unless your child is eligible for special allowance. See more information here.','info',true);
g_form.showFieldMsg('insurance_plan','A child can be enrolled in insurance up until the calendar year in which they turn 25 unless employed or married. Please confirm below.','info',true);
}
else{
g_form.setMandatory('certification_one',false);
g_form.setVisible('certification_one',false);
g_form.hideFieldMsg('claim_dependency',true);
g_form.hideFieldMsg('insurance_plan',true);
}
}
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2021 01:15 AM
Hi
Thank you so much for your time and patience. It is working as expected now. I have marked your response as helpful and correct, I have annother query but I'll post a separate Question for that tagging you 🙂
Regards,
Anshi

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2021 01:27 AM
Happy that issue is resolved. I'll check the other question
Palani