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
aklkkaklkk 

how to writa a Trigger on Parent Object after insert ,after update,after delete trigger worked

Hi all ,

i am facting problem because i am not able to write a trigger for after insert after update after delete parent to child object

How to writa a Trigger on Parent Object  after insert ,after update,after delete trigger worked??

Thanks
aklkk
Vishal_GuptaVishal_Gupta
Hello,

If you want to execute some logic on parent object record on after insert/update/delete of child record then you can directly write your code in child object trigger there is no need to create trigger on Parent object for that.

Please provide more details about your requirement if above information will not help you.
aklkkaklkk
HI Vishal_Gupta

just i want to that if i update the parent records then same records reflect on child recrod i mean(update child record). with the trigger
for example i am change the Account Name values records then same change in contact records values lastName .

Thanks in Avance
Vishal_GuptaVishal_Gupta
For that, in Parent trigger you need to query the child records and need to update the values in fields and then need to fire the Update DML on child records.
Ajay K DubediAjay K Dubedi
Hi Aklkk,

You can use the below code:  

<<<<<<-----Apex class ------>>>>>
 
public class AccountTrigger {
    public static void accountInsert(List<Account> accList){
        try{
            List<Contact> conList = new List<Contact>();
            if(accList.size() > 0){
                for(Account acc : accList){
                    for(Integer i=0; i<2; i++){
                        Contact con = new Contact();
                        con.LastName = 's'+i;
                        con.AccountId = acc.Id;
                        conList.add(con);
                    }
                }
                insert conList;
            }
            
        }catch(Exception exp){
            System.debug('Message'+exp.getMessage());
        }
    }
    public static void accountUpdate(List<Account> accList){
        try{
            if(accList.size() > 0){
                Set<Id> ids = new Set<Id>();
                for(Account acc : accList){
                    ids.add(acc.Id);
                }
                List<Contact> conList = new List<Contact>();
                conList = [SELECT LastName,AccountId FROM Contact WHERE AccountId IN : ids LIMIT 50000];
                for(Account accObj : accList){
                    for(Contact conObj : conList){
                        if(accObj.Id == conObj.AccountId){
                            conObj.LastName = conObj.LastName + accObj.Name;
                        }
                    }
                }
                Update conList;
            }
            
        }catch(Exception exp){
            System.debug('Message'+exp.getMessage());
        }
    }
    public static void accountDelete(List<Account> accList){
        try{
            System.debug('acc'+accList);
            if(accList.size() > 0){
                Set<Id> ids = new Set<Id>();
                for(Account acc : accList){
                    ids.add(acc.Id);
                }
                List<Contact> conList = new List<Contact>();
                conList = [SELECT LastName,AccountId FROM Contact WHERE AccountId IN : ids LIMIT 50000];
                delete conList;
            }
            
        }catch(Exception exp){
            System.debug('Message'+exp.getMessage());
        }
    }
}



<<<<<-----Trigger------>>>>
 
trigger TriggerAccount on Account (after insert, after update, after delete) {
if(Trigger.isAfter){
        if(Trigger.isInsert){
            AccountTrigger.accountInsert(trigger.new);
        }else if(Trigger.isUpdate){
            AccountTrigger.accountUpdate(trigger.new);
        }else{
            AccountTrigger.accountDelete(trigger.old);
        }
    }
}

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

Thanks,
Ajay Dubedi