function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HARISH S 11HARISH S 11 

a trigger on contact object to get the number of days Lived

Hello - Could someone help me with this trigger, a trigger on contact object to get the number of days Lived, take two custom fields 1) Date of birth, 2)Number of days Lived, populate the days in second field.
NagendraNagendra (Salesforce Developers) 
Hi Harish,

Please find the below link from the success community and change the created date field to your custom date of birth field which will populate the number of days lived. Please let us know if you have any issues.

Mark this as resolved if the information helps.

Thanks,
Nagendra
Akshay_DhimanAkshay_Dhiman
Hi Harish,
Below is a code for the same. I have created 2 custom field (DOB__C & No_of_Days_Lived__C) on Contact Object .
Apex Trigger →
trigger ContactTrigger on Contact (before insert , before update) {
    if(Trigger.isbefore && Trigger.isinsert){
        DaysLivedCheck.methodcheck(Trigger.new);
    }
	else if(Trigger.isbefore && Trigger.isupdate){
        DaysLivedCheck.methodcheck(Trigger.new);
    } 
}

Apex Class --->
public class DaysLivedCheck {
    public static void methodcheck(List <Contact> contactlist){  
        for(Contact c: contactlist)
        {
            Date d1=System.today();
            if(c.DOB__c!=null)
            {
                Integer diff=c.DOB__c.daysBetween(d1);
                c.Number_of_days_lived__c=diff;
            }
        }
    }
}

Below is the attached screenshot for the same ​

User-added image

Regards,
Akshay