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
Irish@accIrish@acc 

Trigger to update grand child

Hi, I have written a trigger to update the grandchild whenevr the field in grand parent object updated..this trigger is however not updating the field ..please help me on this

 

trigger updateDev_Lead on Project__c(after update)

{          

 

for(project__c prj: trigger.new)

    {   

 

  if(trigger.oldmap.get(prj.Id).Dev_Lead_User__c!=trigger.newmap.get(prj.Id).Dev_Lead_User__c)

 

{  

 

 List<Task__c> Lsttask=new List<Task__c>();  

 

 

   Lsttask=[Select id,project__c from Task__c where project__c=: prj.Id]; 

 

          for(Task__c thistask: Lsttask)

        {      

 

 List<Root_Cause__c> lstRc=new List<Root_cause__c>();   

 

  lstRc=[select id,Dev_Lead__c from Root_Cause__c where id =:thistask.id];

 

    List<Root_cause__c> RcToupdate=new List<Root_cause__c>();

 

                for(Root_cause__c thisRc:lstRc)     

    {      

               thisRc.Dev_lead__c=prj.Dev_Lead_User__c;  

                                      RcToupdate.add(thisRc);     

 

            }     

   

    if(!RcToupdate.isempty()){ 

          update RcToupdate;   

     }  

   }  

}     

     

}

}

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

In this section:

 

List<Root_Cause__c> lstRc=new List<Root_cause__c>();   
 
  lstRc=[select id,Dev_Lead__c from Root_Cause__c where id =:thistask.id];
 
    List<Root_cause__c> RcToupdate=new List<Root_cause__c>();

 

you are selecting the root causes whose ID matches the task id - this is guaranteed to return no rows as no root causes will have that Id.  You should be checking the related task id. Something like:

 

List<Root_Cause__c> lstRc=new List<Root_cause__c>();   
 
  lstRc=[select id,Dev_Lead__c from Root_Cause__c where Task_id__c =:thistask.id];
 
    List<Root_cause__c> RcToupdate=new List<Root_cause__c>();

This assumes the id of the related task is stored in the Task_Id__c field - if its a different name, substitute that here.

All Answers

bob_buzzardbob_buzzard

In this section:

 

List<Root_Cause__c> lstRc=new List<Root_cause__c>();   
 
  lstRc=[select id,Dev_Lead__c from Root_Cause__c where id =:thistask.id];
 
    List<Root_cause__c> RcToupdate=new List<Root_cause__c>();

 

you are selecting the root causes whose ID matches the task id - this is guaranteed to return no rows as no root causes will have that Id.  You should be checking the related task id. Something like:

 

List<Root_Cause__c> lstRc=new List<Root_cause__c>();   
 
  lstRc=[select id,Dev_Lead__c from Root_Cause__c where Task_id__c =:thistask.id];
 
    List<Root_cause__c> RcToupdate=new List<Root_cause__c>();

This assumes the id of the related task is stored in the Task_Id__c field - if its a different name, substitute that here.

This was selected as the best answer
Irish@accIrish@acc
Thnaks bob for the reply..it works fine