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
Team WorksTeam Works 

Bulkified Trigger to on child object to check the values and then update

Hello All,

I have to wite a Trigger on a Object Override which has a lookup to Contract Object. When a new override is created it should check all the override already existing on the contract and make them inactive. The one which is created latest should be active...
Any help is appreciated.

Many Thanks!
suyog dongaonkarsuyog dongaonkar
I hope following algorithm might help:

for each object override in trigger.new {
create a map of contract id to a list of corresponding child Object Override records (say it map1 = new map<Id, List<ObjectOverride>>);
create a list of all Object Override record ids in trigger.new(say it list1= new list<id>);
}

list<object override> list2 = [get all the Object Override records where contract id in map1.keyset() and object override id not in list1];

for each object override in list2{
deactivate objec override
}
update list2
dphillippi1.388067067032249E12dphillippi1.388067067032249E12
1) Create a 'before insert' trigger
2) Get the Ids of all Parent Contacts in a set
3) Query for all existing ObjectOverrides with that parent and are currently active ([SELECT Active FROM ObjectOverride WHERE Active = true AND Contract__c IN :contractIds])
4) Loop over those existing overrides and set Active to false
5) Update those existing overrides
Team WorksTeam Works
Thanks a lot Phillipp and Suyog. I prefer the one adviced by Phillipp as i need trigger to fire only before insert. In case of updates i think Suyog's solution is a better option. Here is what i have written. Please suggest if any improvements can be made in the following trigger as i am new. Also please guide me how can i write a test class for this.
Many Thanks

trigger Override_Inactive on Override__c (before insert) {

// Whenever a new record is created for override, this trigger looks at the value of status field of all the override records(for a contract) and turns the status to Inactive so that only the newly created override record is an Active one.
Set<Id> contIds = new Set<Id>();
List<contract> parentContract = new List<contract>();
List<override__c> overrideToUpdate = new List<override__c>();
       
        for (Override__c o : trigger.new){
          if (o.Contract__c != null)
             contIds.add(o.Contract__c);
          }
       //system.debug('This is the override records >>>>> ' + contIds);
      
       parentContract  = [select Id,(Select Id, Status__c from contract.Overrides__r )from contract where Id IN :contIds];

       for(Contract contr :parentContract){
          for(override__c oride: contr.overrides__r){
             //system.debug('This is the override records >>>>> ' + oride.Status__c);
             if(oride.Status__c != 'Inactive' && oride != null){
                oride.Status__c = 'Inactive';        
                overrideToUpdate.add(oride);            
               
                }
         }
       }
              If(overrideToUpdate.size() > 0)
               update overrideToUpdate;
}
Team WorksTeam Works
Hello,
I now want to upload this code to Production. How can i put the Test class for this. Please suggest. Any suggestion on the above trigger code is appreciated.

Many Thanks for help.