Calculated Value Field- Estimated Age
Collapse
X
-
Thanks everyone! You guys are awesome. I just tried the formula from Jakub Grufik and it worked. It does require you to update and save the record but that's ok. Once a record is saved, the estimated age displays. Thanks again!! -
Hey @harrytruman
Here is the code you need to make it work, one thing is i didn't understand how do you get the (22) is this a field in your database if yes then add the field to the age calculation in the formula below, please note that if you have a version which is older than 7.4 then you need to use the first code otherwise use the second code:
Code for Versions less then 7.4
PHP Code:// I assume the field is a date field therefore extract the year from the medicalSchoolGraduationYear like below
$medicalSchoolGraduationYear = datetime\year(medicalSchoolGraduationYear);
// Get the current year
$currentYear = datetime\year(datetime\today());
// calculate the age here
$age = ($currentYear - $graduationYear) + 22;
// Expression used in versions less than 7.4
ifThenElse(
$medicalSchoolGraduationYear && $medicalSchoolGraduationYear < $currentYear,
entity\setAttribute('estimatedAge', $age),
entity\setAttribute('estimatedAge', null)
);
Code for Version 7.4 and above
PHP Code:// I assume the field is a date field therefore extract the year from the medicalSchoolGraduationYear like below
$medicalSchoolGraduationYear = datetime\year(medicalSchoolGraduationYear);
// Get the current year
$currentYear = datetime\year(datetime\today());
// calculate the age here
$age = ($currentYear - $graduationYear) + 22;
// Expression used in version 7.4 and above
if ($medicalSchoolGraduationYear && $medicalSchoolGraduationYear < $currentYear) {
entity\setAttribute('estimatedAge', $age);
} else {
entity\setAttribute('estimatedAge', null);
}
Hope this helpsLeave a comment:
-
Hello
Here you go:
Put this into formula of the entity and then recalculate formula for all the records.Code:ifThen( medicalSchoolGraduationYear != null, estimatedAge = (datetime\year(datetime\today()) - medicalSchoolGraduationYear) + 22 );
Please keep in mind that you have to create fields integers called "medicalSchoolGraduationYear" and "estimatedAge" where you will store those values.Last edited by Jakub Grufik; 07-27-2023, 07:51 AM.Leave a comment:
-
Variables uses $ in EspoCRM formulas
Leave a comment:
-
Calculated Value Field- Estimated Age
Hi Everyone,
I'm having trouble writing a formula for a calculated value field.
Here's what I'm trying to do:
-Calculate the estimated age of person/record using their medical school graduation year
-Use a field called 'medicalSchoolGraduationYear' that contains the year they graduated
-Get current year
-Assume they were 22 years old at graduation
-Calculation: Current year - graduation year +22 For example, 2023- 2000+ 22= 45 years old
-Display calculated value (estimated age) in a field called 'estimatedAge'
-This would display anytime a record is viewed by a user, not only when it is saved.
Here's what Chat GPT suggests (does not work so far):
var graduationYear = parseInt(fields['graduation_year']); // Assuming 'graduation_year' is the name of the graduation year field
var currentYear = new Date().getFullYear();
if (graduationYear && graduationYear <= currentYear) {
fields['estimatedAge'] = currentYear - graduationYear + 22;
} else {
fields['estimatedAge'] = null; // Set the field to null if graduation year is missing or in the future
}
Any suggestions? Thanks so much!
Last edited by harrytruman; 07-26-2023, 05:41 PM.Tags: None

Leave a comment: