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
Shubham Sinha 43Shubham Sinha 43 

Write a trigger to update parent record from child record

I need to update parent record from child record. So, I have  four objects- Case, WorkOrder, ServiceAppointmen, ServiceReport. I need to write a trigger when ServiceReport record is created then field (Test__c)  of Case object should be checked. Case is parent object of  Workorder, Workorder is parent Object of ServiceAppointment, ServiceAppointment is Parent object of Service report. I have written a trigger and able to upate the field of ServiceAppointment when service report is created but I need to update the field of case .So, I am unable to fetch the Case record's detail from my triggger and do the updation.

trigger :-
trigger sendEmail on ServiceReport (After insert) {
            list<Id> SerApp = new list<id>();
    for(ServiceReport sr : trigger.new){
        SerApp.add(sr.Parentid); 
    }
             list<ServiceAppointment> sa = [SELECT id,Report_Generated__c from ServiceAppointment WHERE id in: SerApp];
      for(ServiceAppointment a : sa){
                a.Report_Generated__c = true;
              }
         update sa;


}

 
Best Answer chosen by Shubham Sinha 43
Deepali KulshresthaDeepali Kulshrestha
Hi Shubham,

I have gone through your question. To update the record of the case first you have a query each parent object one by one.

Please try the code given below:- 
trigger sendEmail on ServiceReport (after insert){
    List<Case> upList   = new List<Case>();
    Set<ID>  prtID = new Set<ID>();
    for(ServiceReport c: trigger.new){
        if(c.ServiceAppointment != null ){
            prtID.add(c.ServiceAppointment);
        }
    }
    Set<ID> gPId = new Set<Id>();
     List<ServiceAppointment>   prtlist = [Select id,Name,WorkOrder  from ServiceAppointment where id in : prtID ];
    for(ServiceAppointment p: prtlist){
        if(p.WorkOrder != null){
            gPId.add(p.WorkOrder);
        }
    } 
    Set<ID> ggPId = new Set<Id>();
     List<WorkOrder>   gprtlist = [Select id,Name,Case from WorkOrder where id in : gPId ];
    for(WorkOrder gp: gprtlist){
        if(gp.Case != null){
            ggPId.add(gp.Case);
        }
    } 
    List<Case>   ggprtlist = [Select id,Name,Test__c from Case where id in : ggPId ];
    for(Case  gg : ggprtlist ){
        gg.Test__c = true;
        upList.add(gg);
    }
    update upList;
}


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

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com 

All Answers

Danish HodaDanish Hoda
Hi Shubham,
You can refer below code. Please check the fields API names, as there might be some differences, as my org is not FSL enabled:
trigger sendEmail on ServiceReport (After insert) {
	Map<Id, ServiceAppointment> ServiceAppointmentMap = new Map<Id, ServiceAppointment>();
	Map<Id, WorkOrder> WorkOrderMap = new Map<Id, WorkOrder>();
	Map<Id, Case> caseMap = new Map<Id, Case>();
	List<Case> lstCases = new List<Case>();
	
    for(ServiceReport sr : trigger.new){
         ServiceAppointmentMap.put(sr.ServiceAppointmentMap.Id, null);
    }
    if(!ServiceAppointmentMap.isEmpty()){
	for(ServiceAppointment sa : [SELECT Id, WorkOrder FROM ServiceAppointment WHERE Id IN :ServiceAppointmentMap.keySet()]{
		WorkOrderMap.put(sa.WorkOrder.Id, null);
	}
	}
	if(!WorkOrderMap.isEmpty()){
	for(WorkOrder wo : [SELECT Id, CaseId FROM WorkOrder WHERE Id IN :WorkOrderMap.keySet()]{
		caseMap.put(wo.CaseId, null);
	}
	}
	if(!caseMap.isEmpty()){
	for(case caseObj : [SELECT Id, test__c FROM Case WHERE Id IN :caseMap.keyset()]{
		caseObj.test__c = True;
		lstCases.add(caseObj);
		caseMap.put(caseObj.Id, caseObj);
	}
	}
	
	if(!lstCases.isEmpty())	update lstCases;

}

 
Deepali KulshresthaDeepali Kulshrestha
Hi Shubham,

I have gone through your question. To update the record of the case first you have a query each parent object one by one.

Please try the code given below:- 
trigger sendEmail on ServiceReport (after insert){
    List<Case> upList   = new List<Case>();
    Set<ID>  prtID = new Set<ID>();
    for(ServiceReport c: trigger.new){
        if(c.ServiceAppointment != null ){
            prtID.add(c.ServiceAppointment);
        }
    }
    Set<ID> gPId = new Set<Id>();
     List<ServiceAppointment>   prtlist = [Select id,Name,WorkOrder  from ServiceAppointment where id in : prtID ];
    for(ServiceAppointment p: prtlist){
        if(p.WorkOrder != null){
            gPId.add(p.WorkOrder);
        }
    } 
    Set<ID> ggPId = new Set<Id>();
     List<WorkOrder>   gprtlist = [Select id,Name,Case from WorkOrder where id in : gPId ];
    for(WorkOrder gp: gprtlist){
        if(gp.Case != null){
            ggPId.add(gp.Case);
        }
    } 
    List<Case>   ggprtlist = [Select id,Name,Test__c from Case where id in : ggPId ];
    for(Case  gg : ggprtlist ){
        gg.Test__c = true;
        upList.add(gg);
    }
    update upList;
}


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

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com 
This was selected as the best answer