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
Sonu06Sonu06 

Birthdate Update

Hi guys I am new to the salesforce, my requirement is I want to write a trigger.

Trigger:- I want to update the birthday field Of Account with the Birthday of recently(latest) created Contact birthday of respective Account.
(this should work on insert, update and delete)
example- Suppose my account has 3 contacts created 1,2,3 in a row that means 3 is my recently created contact, so suppose  I delete 3rd contact, then birthday of account should be change to birthday of 2nd contact .

Suppose i update the account of contact, then respective birthday should be change with latest contact birthday.

Note- plz do not share any link, if possible give me explanation with trigger.

Thank you
Ajay K DubediAjay K Dubedi
Hi Sonal,

I have soled your question.
Note= I have create custom field Birthdate in Account Object.

----Trigger-----
trigger BrthDyOncontactvsAccount on Contact (After Insert , After delete, After Update) {
    if(trigger.IsAfter && (trigger.IsInsert || trigger.IsUpdate)){
        AccountvsContactBrthdyUpdate.AccountVsContact(trigger.new);
    }
    if(trigger.IsAfter && trigger.IsDelete){
        AccountvsContactBrthdyUpdate.AccountVsContact(trigger.old);
    }
}

----Apex Class--------
public class AccountvsContactBrthdyUpdate {
    public static void AccountVsContact(List<Contact> contactList){
        Set<Id> accountIdSet = new Set<Id>();
        for(Contact contactObj : contactList){
            accountIdSet.add(contactObj.AccountId);
        }
        List<Contact> ConvsAccountList = New List<Contact>();
        ConvsAccountList = [Select Id,
                            AccountId,
                            CreatedDate,
                            Birthdate
                            From Contact 
                            Where AccountId IN : accountIdSet
                            Order By CreatedDate DESC
                            Limit 50000];
        system.debug('conVsAccou--->>'+ConvsAccountList);
        List<Account> accountList = New List<Account>();
        accountList = [Select Birthdate__c
                       From Account
                       Where Id IN : accountIdSet
                       Limit 50000
                      ];
        System.debug('Account-->'+accountList);
        List<Account> accountBrthDyUpdateList = New List<Account>();
        for(Account acc : accountList){
            Integer count = 1;
            for(Contact con : ConvsAccountList){
                if(count == 1 && (acc.Id == con.AccountId)){
                    acc.Birthdate__c = con.Birthdate;
                    count=2;
                    system.debug('latest-->'+con.Birthdate);
                    accountBrthDyUpdateList.add(acc);
                }
            }
        }
        If(accountBrthDyUpdateList.Size() > 0){
            Update accountBrthDyUpdateList;
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com