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
Rafa FedererRafa Federer 

Can some one suggest how we can do the following code : Account and Opportunity related trigger

We have a parent object ABC which has a checkbox parent checkbox , we have a child object obc which has a checkbox child checkbox .

child checkbox needs parent checkbox to be checked in order to be checked . 
The situation i have is when that is done we shld not allow the parent checkbox to be unchecked when trying to and should give an error.

 
ravi soniravi soni
hi Rafa,
you need to try below trigger for achieve your requirment.
what work this trigger is do.
This trigger will excute when Account(Parent) record will come for insert or update and if isParentCheckBox__c field is checked then it will be checked all Child(Opportunity) records isChildCheckbox__c field  check and if user try to uncheck isParentCheckBox__c then it wil be throw an error message.
//here is trigger
trigger updateChildCheckboxFields on Account (after insert,before update,after update) {
    
    if(trigger.isAfter){
        set<string> accountIds = new set<string>();
        list<Opportunity> lstOpportunities = new list<Opportunity>();
        if(trigger.isInsert || trigger.isUpdate){
            for(Account acc : trigger.new){
                accountIds.add(acc.Id);
            }
            
            for(Account oAcc : [SELECT Id,Name,isParentCheckbox__c,(SELECT Id,Name,isChildCheckbox__c FROM Opportunities) FROM Account Where Id In : accountIds]){
            if(oAcc.isParentCheckbox__c){
                for(Opportunity opp : oAcc.Opportunities){
                    opp.isChildCheckbox__c = true;
                    lstOpportunities.add(opp);
                }
            }
            }
            if(lstOpportunities.size() > 0){
                update lstOpportunities;
            }
            
        }
    }
    /* If user try to uncheck field after made it check, will throw error message */
    if(trigger.isBefore && trigger.isUpdate){
        for(Account acc : trigger.new){
            if(acc.isParentCheckbox__c == false && trigger.oldMap.get(acc.Id).isParentCheckbox__c == true){
                acc.addError('you cant  unCheck isParentCheckbox field once made it check');
            }
            }
        
    }
    

}

if you satisified with my code then don't forget to mark it as best answer.
I believe you will be marked it as best.
Thank you
Rafa FedererRafa Federer
Hey Veer , for the second part i have used a before update . My first part is on opportunity . 

Second part i have written this way : 

Can you suggest how i could avoid the nested for loop here ????

set<id> accountid = new set<id> ();
for(account accrecords: trigger.new)
{
accountid.add(accrecords.id);

List<Opportunity> opty = new List<Opportunity>();
opty = [ select id . accountid , childcheckbox__c from opportunity where accountid in: accountid];

for(Opportunity optyrec: opty)
{
if(optyrec.childcheckbox__c == true)
{
if(accrecords.parentcheckbox__c == false)
{
accrecords.adderror('cannot uncheck ');
}
}
}
}