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
kishore goud 3kishore goud 3 

Trigger to update check box in parent when child picklist is updated based on parent picklist

Suppose there is parent object(A) and child object (B).When I select picklist value in parent ,picklist in child should be updated and later check box in parent should be updated.For example picklist values of A(D1,D2) and picklist values of B(Division 1,Division 2).If we select picklist values as D1 and then all the picklist value of all child objects should be Divsion 1 and later check box in parent should be checked.Could any one please post a trigger for this scenario.
Best Answer chosen by kishore goud 3
v varaprasadv varaprasad
Trigger updateChilds on parentObject__c(after update){
  map<id,parentObject__c> parentIds = new map<id,parentObject__c>();
  public static boolean checkUpdaate = false;
 
 for(parentObject__c pr : trigger.new){
	  if(pr.parentpicklist__c != null && checkUpdaate == false){
		 parentIds.put(pr.id,pr);
	  }
 }

  list<childObject__c> lstOfChilds = [select id,childpicklist__c from childObject__c where parentid in : parentIds.keyset()];
  list<childObject__c> updOfChilds = new list<childObject__c>();
  
	for(childObject__c chld : lstOfChilds){
	   string parentDIvsion  = parentIds.get(chld.parentIds).parentpicklist__c)
	   if(parentDIvsion == 'D1' && chld.childpicklist__c != 'Division1'){ 		   
		   chld.childpicklist__c = 'Division1';			   
		}else if(parentDIvsion == 'D2' && chld.childpicklist__c != 'Division2'){
		  chld.childpicklist__c = 'Division2';	
		}
		lstOfChilds.add(chld);
	}

	list<parentObject__c> updatPlist = new  list<parentObject__c>();
   if(lstOfChilds.size()>0){
	  update lstOfChilds;
	  
	  list<parentObject__c> plist = [select id ,check__c from parentObject__c where id in : parentIds.keyset()];
	  for(parentObject__c pl : plist){
		  if(pl.check__c != True){
			pl.check__c = True;
			updatPlist.add(p1);				
		  }			  
	  }
	  if(updatPlist.size()>0){
		update plist;
		checkUpdaate = True;
	  }
   }
   
}
For best practices use above code.


Thanks
Varaprasad 

All Answers

v varaprasadv varaprasad
Hi Kishore,

Please find sample code below : 

 

Trigger updateChilds on parentObject__c(after update){
      map<id,parentObject__c> parentIds = new map<id,parentObject__c>();
     
	 for(parentObject__c pr : trigger.new){
	      if(pr.parentpicklist__c != null){
		     parentIds.put(pr.id,pr);
		  }
	 }

      list<childObject__c> lstOfChilds = [select id,childpicklist__c from childObject__c where parentid in : parentIds.keyset()];
	  list<childObject__c> updOfChilds = new list<childObject__c>();
	  
        for(childObject__c chld : lstOfChilds){
		   string parentDIvsion  = parentIds.get(chld.parentIds).parentpicklist__c)
           if(parentDIvsion == 'D1' && chld.childpicklist__c != 'Division1'){ 		   
			   chld.childpicklist__c = 'Division1';			   
			}else if(parentDIvsion == 'D2' && chld.childpicklist__c != 'Division2'){
			  chld.childpicklist__c = 'Division2';	
			}
			lstOfChilds.add(chld);
		}

       
	   if(lstOfChilds.size()>0){
	      update lstOfChilds;
		  
		  list<parentObject__c> plist = [select id ,check__c from parentObject__c where id in : parentIds.keyset()];
		  for(parentObject__c pl : plist){
		      pl.check__c = True;		  
		  }
		  update plist;
	   }
	   
	   


}

Hope it helps you.
If it helps you please mark it as the best answer.
Please let me know in case of any other help.

Thanks
Varaprasad

 
kishore goud 3kishore goud 3
Hi Vara,
As per the requirement .
Part 1: When I select picklist value in parent ,picklist in child should be updated and
Part 2: Later check box in parent should be updated.

I able to complete part 1 with the code you posted and lines of code is from Line 1 to Line 27.I changed the code from line 27 because I was even faicng the issue with the code posted by you .So, I changed the code , but still facing the issue.I mentioned the error below.Please help me in resolving the issue.


1 Trigger updateChilds on Service_Item__c(after update){
2       map<id,Service_Item__c> parentIds = new map<id,Service_Item__c>();
3      
4      for(Service_Item__c    pr : trigger.new){
5           if(pr.Area__c != null){
6              parentIds.put(pr.id,pr);
7           }
8      }

10      list<Item__c> lstOfChilds = [select id,Division__c,SI__c  from Item__c where SI__c in : parentIds.keyset()];
11      list<Item__c> updOfChilds = new list<Item__c>();
12      
13        for(Item__c chld : lstOfChilds){
14           string Area  = parentIds.get(chld.SI__c).Area__c;
15           if(Area == 'D1' && chld.Division__c != 'Division1'){            
16               chld.Division__c = 'Division1';             
17            }else if(Area == 'D2' && chld.Division__c != 'Division2'){
18              chld.Division__c = 'Division2';   
19            }
20            updOfChilds.add(chld);
21        }
22
23       
24       if(updOfChilds.size()>0){
25          update updOfChilds;
26          
27        }
28        
29        Set<ID> IDSET = new Set<ID>();
30        for(Item__c itemLst:updOfChilds ){
31            IDSET.add(itemLst.SI__c);
32        }
33      
34        List<Service_Item__c> latestLst = new List<Service_Item__c>();
35        for(Service_Item__c srvLst :[select id ,CheckDivsion__c from Service_Item__c where id in : IDSET]){
36            srvLst.CheckDivsion__c=true;
37            latestLst.add(srvLst);
38            
39        }
40        
41        if(latestLst.size()>0){
42            update latestLst;
43        }       
44
45}




Error:Apex trigger updateChilds caused an unexpected exception, contact your administrator: updateChilds: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a1K28000002JJ5PEAW; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateChilds: maximum trigger depth exceeded Service_Item trigger event AfterUpdate for [a1K28000002JJ5P] Service_Item trigger event AfterUpdate for [a1K28000002JJ5P] Service_Item trigger event AfterUpdate for [a1K28000002JJ5P] Service_Item trigger event AfterUpdate for [a1K28000002JJ5P] Service_Item trigger event AfterUpdate for [a1K28000002JJ5P] Service_Item trigger event AfterUpdate for [a1K28000002JJ5P] Service_Item trigger event AfterUpdate for [a1K28000002JJ5P] Service_Item trigger event AfterUpdate for [a1K28000002JJ5P] Service_Item trigger event AfterUpdate for [a1K28000002JJ5P] Service_Item trigger event AfterUpdate for [a1K28000002JJ5P] Service_Item trigger event AfterUpdate for [a1K28000002JJ5P] Service_Item trigger event AfterUpdate for [a1K28000002JJ5P] Service_Item trigger event AfterUpdate for [a1K28000002JJ5P] Service_Item trigger event AfterUpdate for [a1K28000002JJ5P] Service_Item trigger event AfterUpdate for [a1K28000002JJ5P] Service_Item trigger event AfterUpdate for [a1K28000002JJ5P]: []: Trigger.updateChilds: line 42, column 1
v varaprasadv varaprasad
Hi Kishore,

Please check once below code : 
 
Trigger updateChilds on parentObject__c(after update){
      map<id,parentObject__c> parentIds = new map<id,parentObject__c>();
     
	 for(parentObject__c pr : trigger.new){
	      if(pr.parentpicklist__c != null){
		     parentIds.put(pr.id,pr);
		  }
	 }

      list<childObject__c> lstOfChilds = [select id,childpicklist__c from childObject__c where parentid in : parentIds.keyset()];
	  list<childObject__c> updOfChilds = new list<childObject__c>();
	  
        for(childObject__c chld : lstOfChilds){
		   string parentDIvsion  = parentIds.get(chld.parentIds).parentpicklist__c)
           if(parentDIvsion == 'D1' && chld.childpicklist__c != 'Division1'){ 		   
			   chld.childpicklist__c = 'Division1';			   
			}else if(parentDIvsion == 'D2' && chld.childpicklist__c != 'Division2'){
			  chld.childpicklist__c = 'Division2';	
			}
			lstOfChilds.add(chld);
		}

        list<parentObject__c> updatPlist = new  list<parentObject__c>();
	   if(lstOfChilds.size()>0){
	      update lstOfChilds;
		  
		  list<parentObject__c> plist = [select id ,check__c from parentObject__c where id in : parentIds.keyset()];
		  for(parentObject__c pl : plist){
		      if(pl.check__c != True){
		        pl.check__c = True;
                updatPlist.add(p1);				
              }			  
		  }
		  if(updatPlist.size()>0){
		    update plist;
		  }
	   }
	   
	   


}

Hope it helps you.


Thanks
Varaprasad









 
v varaprasadv varaprasad
Trigger updateChilds on parentObject__c(after update){
  map<id,parentObject__c> parentIds = new map<id,parentObject__c>();
  public static boolean checkUpdaate = false;
 
 for(parentObject__c pr : trigger.new){
	  if(pr.parentpicklist__c != null && checkUpdaate == false){
		 parentIds.put(pr.id,pr);
	  }
 }

  list<childObject__c> lstOfChilds = [select id,childpicklist__c from childObject__c where parentid in : parentIds.keyset()];
  list<childObject__c> updOfChilds = new list<childObject__c>();
  
	for(childObject__c chld : lstOfChilds){
	   string parentDIvsion  = parentIds.get(chld.parentIds).parentpicklist__c)
	   if(parentDIvsion == 'D1' && chld.childpicklist__c != 'Division1'){ 		   
		   chld.childpicklist__c = 'Division1';			   
		}else if(parentDIvsion == 'D2' && chld.childpicklist__c != 'Division2'){
		  chld.childpicklist__c = 'Division2';	
		}
		lstOfChilds.add(chld);
	}

	list<parentObject__c> updatPlist = new  list<parentObject__c>();
   if(lstOfChilds.size()>0){
	  update lstOfChilds;
	  
	  list<parentObject__c> plist = [select id ,check__c from parentObject__c where id in : parentIds.keyset()];
	  for(parentObject__c pl : plist){
		  if(pl.check__c != True){
			pl.check__c = True;
			updatPlist.add(p1);				
		  }			  
	  }
	  if(updatPlist.size()>0){
		update plist;
		checkUpdaate = True;
	  }
   }
   
}
For best practices use above code.


Thanks
Varaprasad 
This was selected as the best answer
kishore goud 3kishore goud 3
Thanks for the help ,It got resolved
v varaprasadv varaprasad
Hi Kishore,

Above information helps you please mark it as the best answer.

Thanks
Varaprasad