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
Arnold Joseph TodasArnold Joseph Todas 

How can i retain checkbox in parent ?

Hello,

How can i retain checkbox in parent, I have picklist in child containing ('a','b','c','d','e'). I created a child with a picklist of 'a' and it should checked on parent and i delete the child, the checkbox 'a' of parent should be uncheked  . the main problem is if i created another child that contains picklist 'a', and i delete the first child which is picklist 'a' the checkbox of parent will retain because there is another child that i created with picklist 'a' . how can i create a trigger for that?

Thanks for help!
AJ
Pratyush Kumar 15Pratyush Kumar 15
Hey Arnold, This feature can be achieved using Apex Triggers on your child object. You would need to add logic to run on insert / update / delete / undelete events.
ManojjenaManojjena
Hi Arnold,

Can you confirm one thing like you want only one check box for pick list a or 5 check box for a,b,c,d,e   .Please confirm .
Arnold Joseph TodasArnold Joseph Todas
Hello Manoj,

I have 5 checkbox for A B C D E on picklist.
ManojjenaManojjena
Hi Arnold ,

Below trigger will work for checkbox A and picklist A ,Try to implement for next four .

trigger ParentCheckBox on Child (After insert ,After update,After delete ,After undelete) {
  Map<id,String> idpicklistvalueMap=new Map<Id,String>();
   if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete){
  for(Child chd : Trigger.new){
    if(chd.ChildChekbox__c != null){
     idpicklistvalueMap.put(chd.AccountId,chd.ChildChekbox__c);
    }
    }
   }if(Trigger.isDelete){
 for(Child chd : Trigger.old){
    if(chd.ChildChekbox__c != null){
     idpicklistvalueMap.put(chd.AccountId,chd.ChildChekbox__c);
    }
    }
   }
  
   List<Parent> patListToUpdate=new List<Parent>();
   for(Parent pat :[SELECT id,(SELECT ChildChekbox__c FROM Contacts WHERE ChildChekbox__c='A') FROM Parent WHERE id IN : idpicklistvalueMap.keySet()]){
      if(pat.ChildRelationshipName.size() > 0){
         Parent prt=new Parent(id=pat.Id,ParentChkBox__c=true);
            patListToUpdate.add(prt);
      }else{
         Parent prtt=new Parent(id=prtt.Id,ParentChkBox__c=false);
             patListToUpdate.add(prtt);
      }
   }
   try{
     update patListToUpdate;
   }catch(DMLEXception de){
      System.debug(de);
   }
}

Any issue let me know .
Arnold Joseph TodasArnold Joseph Todas
public static void checkMethodType(List<Sample_Program__c> listsampleProgram) {
Set<Id> contractIds = new Set<Id>();
       for(Sample_Program__c sp: listsampleProgram){
            contractIds.add(sp.Contract_ID__c);
       }
       System.debug('*#*' + contractIds);
       List<Contract> cont = [SELECT Id, Composite__c, Dioxin__c, Metal_Scan__c, Grab__c, Organic_Scan__c,( SELECT Method_Type__c FROM Sample_Programs__r )FROM Contract WHERE Id IN: contractIds];
        for(Contract cnt: cont){
            if(cnt.Sample_Programs__r != Null){
                for(Sample_Program__c sampleRec: cnt.Sample_Programs__r){
                    if(sampleRec.Method_Type__c == 'Grab'){
                        cnt.Grab__c = true;
                    }else if(sampleRec.Method_Type__c == 'Composite'){
                        cnt.Composite__c = true;
                    }else if(sampleRec.Method_Type__c == 'Metal Scan'){
                        cnt.Metal_Scan__c = true;
                    }else if(sampleRec.Method_Type__c == 'Dioxin'){
                        cnt.Dioxin__c = true;
                    }else if(sampleRec.Method_Type__c == 'Organic Scan'){
                        cnt.Organic_Scan__c = true;
                    }
            }
        }

  }update cont;
}
    public static void uncheck(List<Sample_Program__c> listsampleProgram) {
       Set<Id> contractIds = new Set<Id>();
       Set<Id> samplePrograms = new Set<Id>();
       Set<String> s = new Set<String>();
       for(Sample_Program__c sp: listsampleProgram){
            contractIds.add(sp.Contract_ID__c);
            samplePrograms.add(sp.Id);
       }

       Boolean existRec = false;
       List<Contract> cont = [SELECT Id, Composite__c, Dioxin__c, Metal_Scan__c, Grab__c, Organic_Scan__c, ( SELECT Method_Type__c FROM Sample_Programs__r WHERE Id IN: samplePrograms)FROM Contract WHERE Id IN: contractIds];


        for(Contract cnt: cont){
            if(cnt.Sample_Programs__r.size() != 0){
                for(Sample_Program__c sampleRec: cnt.Sample_Programs__r){
                    s.add(sampleRec.Method_Type__c);

            }
        }
        System.debug(s);

         }
          List<Contract> cont1 = [SELECT Id, Composite__c, Dioxin__c, Metal_Scan__c, Grab__c, Organic_Scan__c, ( SELECT Method_Type__c FROM Sample_Programs__r)FROM Contract WHERE Id IN: contractIds];
            
            for(Contract cnt: cont1){
                    if(cnt.Sample_Programs__r != Null){
                for(Sample_Program__c sampleRec: cnt.Sample_Programs__r){
                     
                    if(sampleRec.Method_Type__c == 'Grab' ){
                         cnt.Grab__c = false;
                    }else if(sampleRec.Method_Type__c == 'Composite'){
                        cnt.Composite__c = false;
                    }else if(sampleRec.Method_Type__c == 'Metal Scan'){
                        cnt.Metal_Scan__c = false;
                    }else if(sampleRec.Method_Type__c == 'Dioxin'){
                        cnt.Dioxin__c = false;
                    }else if(sampleRec.Method_Type__c == 'Organic Scan'){
                        cnt.Organic_Scan__c = false;
                    }
                }

          } 
         
    }update cont1;
}
}
Here is my code the first method is for checking the parent checkbox and the second method is when i delete the child, the parent checkbox is unchecked.. but my problem is if i inserted two child with the same picklist and i deleted one of the child the checkbox is not retaining. can you modified it for me Thanks for your help
ManojjenaManojjena
Hey Arnold ,

Do one thing in all cases just pass the account id set from trigger to handler class method  ,and apply the logic .

 
ManojjenaManojjena
Arnold ,
try below code it will work ,
trigger ContractCheckBox on Sample_Program__c (After insert ,After update,After delete ,After undelete) { 
	   Set<Id> contractIds=new Set<Id>();
	   if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete){
			for(Sample_Program__c sp : Trigger.new){
			  if(sp.Method_Type__c != null){
				 contractIds.add(sp.Contract_ID__c);
			  }
		   }
	   }if(Trigger.isDelete){
		for(Sample_Program__c sp : Trigger.old){
			  if(sp.Method_Type__c != null){
				  contractIds.add(sp.Contract_ID__c);
			  }
		   }
	   }
	List<Contract> cont = [SELECT Id, Composite__c, Dioxin__c, Metal_Scan__c, Grab__c, Organic_Scan__c,( SELECT Method_Type__c FROM Sample_Programs__r )FROM Contract WHERE Id IN: contractIds];
    for(Contract cnt: cont){
		if(cnt.Sample_Programs__r != Null){
			for(Sample_Program__c sampleRec: cnt.Sample_Programs__r){
				if(sampleRec.Method_Type__c == 'Grab'){
					cnt.Grab__c = true;
				}else if(sampleRec.Method_Type__c == 'Composite'){
					cnt.Composite__c = true;
				}else if(sampleRec.Method_Type__c == 'Metal Scan'){
					cnt.Metal_Scan__c = true;
				}else if(sampleRec.Method_Type__c == 'Dioxin'){
					cnt.Dioxin__c = true;
				}else if(sampleRec.Method_Type__c == 'Organic Scan'){
					cnt.Organic_Scan__c = true;
				}
			}
		}
	}
	try{
		update cont;
	}catch(DMLException de ){
	    System.debug('*******'+de);
	}
}



 
Arnold Joseph TodasArnold Joseph Todas
Sir Manoj,

I'm using triggerhandler for that, i'll try converting and i let you know if there are some bugs

Thanks
AJ
Arnold Joseph TodasArnold Joseph Todas
Sir Manoj,

It's not unchecking when i delete both child.. i created two child which is picklist of grab, and i delete it both the checkbox in parent should be uncheck how can i fix that bug? 

Thanks!
AJ