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
Jay16Jay16 

Trigger to update Checkbox on parent record not working

Hi, 

I'm trying to check a checkbox on the parent opportunity when a child record is created. 

When I test the trigger below, the debug log says the checkbox has been updated to true. However when I check the opportunity is has not. I'm quite new to developing in general so apologies if this is something obvious: 

trigger updateOppCb on Bonus_Calculator__c (before insert) {
   
        // Create a set of related opps
        set <id> ids = new set <id>();
        for (Bonus_Calculator__c newSet : Trigger.new)
        ids.add(newSet.opportunity__c);

        // Add child? checkbox to map
        map <id,Opportunity> childMap = new map<id,Opportunity>();
        for (Opportunity o:[select child__c from opportunity where id in :ids])
        childMap.put(o.id, o);
   
     for (Bonus_Calculator__c newBonus : Trigger.new) {
           
            Opportunity o = childMap.get(newBonus.opportunity__c);
            o.child__c = true;
            system.debug('opportunity = '+ o);
   }
}

 

...Thanks a million for any help!

Best Answer chosen by Jay16
Vinit_KumarVinit_Kumar
You are not updating the opportunity record,you need to do that explicitly.

Try below code :-

trigger updateOppCb on Bonus_Calculator__c (before insert) {
        
        List<Opportunity> oppList = new List<Opportunity>();
        // Create a set of related opps
        set <id> ids = new set <id>();
        for (Bonus_Calculator__c newSet : Trigger.new)
        ids.add(newSet.opportunity__c);

        // Add child? checkbox to map
        map <id,Opportunity> childMap = new map<id,Opportunity>();
        for (Opportunity o:[select child__c from opportunity where id in :ids])
        childMap.put(o.id, o);
   
     for (Bonus_Calculator__c newBonus : Trigger.new) {
           
            Opportunity o = childMap.get(newBonus.opportunity__c);
            o.child__c = true;
            system.debug('opportunity = '+ o);
           oppList.add(o);
   }
      // you are missing below lines
      if(oppList.size()>0)
       {
       update oppList;
      }
}
If this helps,please mark it as best answer to help others :)

All Answers

Vinit_KumarVinit_Kumar
You are not updating the opportunity record,you need to do that explicitly.

Try below code :-

trigger updateOppCb on Bonus_Calculator__c (before insert) {
        
        List<Opportunity> oppList = new List<Opportunity>();
        // Create a set of related opps
        set <id> ids = new set <id>();
        for (Bonus_Calculator__c newSet : Trigger.new)
        ids.add(newSet.opportunity__c);

        // Add child? checkbox to map
        map <id,Opportunity> childMap = new map<id,Opportunity>();
        for (Opportunity o:[select child__c from opportunity where id in :ids])
        childMap.put(o.id, o);
   
     for (Bonus_Calculator__c newBonus : Trigger.new) {
           
            Opportunity o = childMap.get(newBonus.opportunity__c);
            o.child__c = true;
            system.debug('opportunity = '+ o);
           oppList.add(o);
   }
      // you are missing below lines
      if(oppList.size()>0)
       {
       update oppList;
      }
}
If this helps,please mark it as best answer to help others :)

This was selected as the best answer
Jay16Jay16
Thanks Vinit, that worked straight away :) Much appreciated! 
Jay16Jay16
If you have time, I have one more question. I just tried to create another trigger using the same code, only this time it is a before delete trigger which sets the checkbox to false. This is so that if the child is deleted, the settings are restored. 

When I tried this i get a 'you have tried to dereference a null pointer...' exception. Do you happen to know why this might be?
Jay16Jay16
Don't worry - i figured it out. I needed to use trigger.old instead. 
Thanks again