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
Sumanta SatpathySumanta Satpathy 

After trigger for update field value in parent object when filed updated in child object where a lookup relation ship exists in between child and parent

A requirement for trigger:
I just want to write one after update trigger.
The scenario is as follows:
Employee_c and Employer_c two custom objects are there.
Employee__c is the child object and Employer_c is the parent object.
Lookup relationship is present in between the objects.
The lookup field is employer name.
When salary is getting updated in the salary_c field of the Employee_c object at that time
Total cost_c field present in the Employer_c object needs to be updated.
Object details:
Child Object:
Object name: Employee__c
Fields: Name
Salary__c
Employeer_Name(lookup to Employer__c)
Parent Object: when Salary__c getting updated
Object Name:Employer__c
Fields: Name
Total_Cost__c
Total_cost__c should be updated
Any suggestion is appretiated.

Thanks in advance
Best Answer chosen by Sumanta Satpathy
Sumanta SatpathySumanta Satpathy
trigger Update_ObjB_Update_ObjA on ObjectB__c (after insert, after delete, after update) {
    decimal amount=0.00;
    set<Id> idSet= new set<Id>();
    if(Trigger.isInsert){
            try {
                for (ObjectB__c ob : Trigger.new){
                    idSet.add(ob.ObjectA_Name__c);
                }
                    ObjectA__C oa = [SELECT Id, Total_Cost__c  FROM ObjectA__C WHERE Id = :idSet];
                    
                    List<ObjectB__c> bList = [SELECT Id, Salary__c FROM ObjectB__c WHERE ObjectA_Name__c = :oa.Id];
                
                    for(ObjectB__c ob : bList) {
                        amount+= ob.Salary__c;  
                    }
                    oa.Total_Cost__c = amount;

                    update oa;
                
            } catch (Exception e) {
                System.debug(e);
            }
        }

    if(Trigger.isAfter) {
        if(Trigger.isUpdate){
            try {
                for (ObjectB__c ob : Trigger.old){
                    idSet.add(ob.ObjectA_Name__c);
                }
                    ObjectA__C oa = [SELECT Id, Total_Cost__c  FROM ObjectA__C WHERE Id = :idSet];
                    
                    List<ObjectB__c> bList = [SELECT Id, Salary__c FROM ObjectB__c WHERE ObjectA_Name__c = :oa.Id];
                
                    for(ObjectB__c ob : bList) {
                       amount+= ob.Salary__c;  
                    }
                    oa.Total_Cost__c = amount;

                    update oa;
                }
             catch (Exception e) {
                System.debug(e);
            }
        }

        if(Trigger.isDelete){
            try {
                for (ObjectB__c ob : Trigger.old){
                    idSet.add(ob.ObjectA_Name__c);
                }
                    ObjectA__C oa = [SELECT Id, Total_Cost__c  FROM ObjectA__C WHERE Id = :idSet];
                    
                    List<ObjectB__c> bList = [SELECT Id, Salary__c FROM ObjectB__c WHERE ObjectA_Name__c = :oa.Id];
                    for(ObjectB__c ob : bList) {
                        amount+= ob.Salary__c;   
                    }

                    oa.Total_Cost__c = amount;

                    update oa;
                }
             catch (Exception e) {
                System.debug(e);
            }
        }
    }
}

I have solved by myself..this the sample code 

All Answers

Sumanta SatpathySumanta Satpathy
trigger Update_ObjB_Update_ObjA on ObjectB__c (after insert, after delete, after update) {
    decimal amount=0.00;
    set<Id> idSet= new set<Id>();
    if(Trigger.isInsert){
            try {
                for (ObjectB__c ob : Trigger.new){
                    idSet.add(ob.ObjectA_Name__c);
                }
                    ObjectA__C oa = [SELECT Id, Total_Cost__c  FROM ObjectA__C WHERE Id = :idSet];
                    
                    List<ObjectB__c> bList = [SELECT Id, Salary__c FROM ObjectB__c WHERE ObjectA_Name__c = :oa.Id];
                
                    for(ObjectB__c ob : bList) {
                        amount+= ob.Salary__c;  
                    }
                    oa.Total_Cost__c = amount;

                    update oa;
                
            } catch (Exception e) {
                System.debug(e);
            }
        }

    if(Trigger.isAfter) {
        if(Trigger.isUpdate){
            try {
                for (ObjectB__c ob : Trigger.old){
                    idSet.add(ob.ObjectA_Name__c);
                }
                    ObjectA__C oa = [SELECT Id, Total_Cost__c  FROM ObjectA__C WHERE Id = :idSet];
                    
                    List<ObjectB__c> bList = [SELECT Id, Salary__c FROM ObjectB__c WHERE ObjectA_Name__c = :oa.Id];
                
                    for(ObjectB__c ob : bList) {
                       amount+= ob.Salary__c;  
                    }
                    oa.Total_Cost__c = amount;

                    update oa;
                }
             catch (Exception e) {
                System.debug(e);
            }
        }

        if(Trigger.isDelete){
            try {
                for (ObjectB__c ob : Trigger.old){
                    idSet.add(ob.ObjectA_Name__c);
                }
                    ObjectA__C oa = [SELECT Id, Total_Cost__c  FROM ObjectA__C WHERE Id = :idSet];
                    
                    List<ObjectB__c> bList = [SELECT Id, Salary__c FROM ObjectB__c WHERE ObjectA_Name__c = :oa.Id];
                    for(ObjectB__c ob : bList) {
                        amount+= ob.Salary__c;   
                    }

                    oa.Total_Cost__c = amount;

                    update oa;
                }
             catch (Exception e) {
                System.debug(e);
            }
        }
    }
}

I have solved by myself..this the sample code 
This was selected as the best answer
Steven NsubugaSteven Nsubuga
Consider using this much shorter version of the code
trigger Update_ObjB_Update_ObjA on ObjectB__c (after insert, after delete, after update) {
    decimal amount=0.00;
    set<Id> idSet= new set<Id>();
	try {
		for (ObjectB__c ob : Trigger.new){
			idSet.add(ob.ObjectA_Name__c);
		}
		if(Trigger.isUpdate || Trigger.isDelete){
			for (ObjectB__c ob : Trigger.old){
				idSet.add(ob.ObjectA_Name__c);
			}
		}
		ObjectA__C oa = [SELECT Id, Total_Cost__c  FROM ObjectA__C WHERE Id = :idSet];
		
		List<ObjectB__c> bList = [SELECT Id, Salary__c FROM ObjectB__c WHERE ObjectA_Name__c = :oa.Id];
	
		for(ObjectB__c ob : bList) {
			amount+= ob.Salary__c;  
		}
		oa.Total_Cost__c = amount;
		update oa;
		
	} catch (Exception e) {
		System.debug(e);
	}    
}