Few months back as a part of Business requirement we need to
calculate AGE on the basis of following criterias:
- Calculate Age on the basis of Next Birthdate
- Calculate Age on the basis of Previous Birthdate
- Calculate Age on the basis of Nearest Birthdate
Initially it was proposed that this
can be achieved only by using trigger. But I had taken this challenge to
accomplish it by using formulae field.
Finally we succeed by creating
formulae for calculating Age based on above 3 criteria.
Following are the steps which we
did for completing this requirement by using formulae field approach:
1. We
already have ‘Birthdate’ standard field on Contact object.
2. We
created a picklist field ‘Age Calculation Criteria’ having below 3 options:
A) Next B'day
B) Last B'day
C) Nearest B'day
3. Created
a formulae field ‘Age’ which have below formulae:
if(TEXT(Age_Calculation_Crieteria__c)=="Last B'day",
if(DATE(YEAR(TODAY()),MONTH(Birthdate),DAY(Birthdate))<= TODAY(), YEAR(TODAY()) - YEAR(Birthdate),YEAR(TODAY()) -1 - YEAR(Birthdate)),
if(TEXT(Age_Calculation_Crieteria__c)=="Next B'day",
if(DATE(YEAR(TODAY()),MONTH(Birthdate),DAY(Birthdate))<= TODAY(), YEAR(TODAY())+1 - YEAR(Birthdate),YEAR(TODAY()) - YEAR(Birthdate)),
if(TEXT(Age_Calculation_Crieteria__c)=="Nearest B'day",
if(Today()>= Birthdate,
if(
(Today()-Date(Year(Today()),Month(Birthdate),Day(Birthdate)))
<=(Date(Year(Today())+1,Month(Birthdate),Day(Birthdate))-Today()),
Year(Today())-Year(Birthdate),Year(Today())+1-Year(Birthdate)),
if((Date(Year(Today()),Month(Birthdate),Day(Birthdate))-Today())<=(Today()-Date(Year(Today())-1,Month(Birthdate),Day(Birthdate))),Year(Today())-Year(Birthdate),Year(Today())-1-Year(Birthdate))),if(DATE(YEAR(TODAY()),MONTH(Birthdate),DAY(Birthdate))<= TODAY(), YEAR(TODAY()) - YEAR(Birthdate),YEAR(TODAY()) -1 - YEAR(Birthdate)))
))
if(DATE(YEAR(TODAY()),MONTH(Birthdate),DAY(Birthdate))<= TODAY(), YEAR(TODAY()) - YEAR(Birthdate),YEAR(TODAY()) -1 - YEAR(Birthdate)),
if(TEXT(Age_Calculation_Crieteria__c)=="Next B'day",
if(DATE(YEAR(TODAY()),MONTH(Birthdate),DAY(Birthdate))<= TODAY(), YEAR(TODAY())+1 - YEAR(Birthdate),YEAR(TODAY()) - YEAR(Birthdate)),
if(TEXT(Age_Calculation_Crieteria__c)=="Nearest B'day",
if(Today()>= Birthdate,
if(
(Today()-Date(Year(Today()),Month(Birthdate),Day(Birthdate)))
<=(Date(Year(Today())+1,Month(Birthdate),Day(Birthdate))-Today()),
Year(Today())-Year(Birthdate),Year(Today())+1-Year(Birthdate)),
if((Date(Year(Today()),Month(Birthdate),Day(Birthdate))-Today())<=(Today()-Date(Year(Today())-1,Month(Birthdate),Day(Birthdate))),Year(Today())-Year(Birthdate),Year(Today())-1-Year(Birthdate))),if(DATE(YEAR(TODAY()),MONTH(Birthdate),DAY(Birthdate))<= TODAY(), YEAR(TODAY()) - YEAR(Birthdate),YEAR(TODAY()) -1 - YEAR(Birthdate)))
))
Test Result: Test Result on 18th Sep, 2015
1. Age Criteria: Nearest B'day
2. BirthDate: 03/10/1955[3rd Oct, 1955]
3. Result for Age: 60
4. Age Crieteria: Last B'day
5. BirthDate: 03/10/1955[3rd Oct, 1955]
6. Result for Age: 59
7. Age Crieteria: Next B'day
8. BirthDate: 03/10/1955[3rd Oct, 1955]
9. Result for Age: 60
Hope this post help you to utilize formulae field capability in Salesforce instead of writting trigger for such type of requirements :)