0

I don't know if I'm doing it right. I have to set checkboxes values to true after import new contracts, but just for the contracts that are related to an account that already has a Contract where the EndDate of any pre-existing contract has the same StartDate of this new Contract (the checkbox of this contract has to be set to true). That is my trigger so far, how can I update these new contracts?

trigger testTrigger on Contract (before insert) {
List<Contract> contracts = [select EndDate from Contract where AccountId= :c.AccountId];    
    for(Contract c: Trigger.new){            
        for (Contract cs: contracts){
            if (c.StartDate == cs.EndDate){
                c.checkboxTest__c=True;
            }
        }   
    }
}
  • 2
    Also, you have a query inside a loop. Be aware this may cause issues with bulk DML operations. – sfdcfox Apr 18 at 1:21
  • 3
    Please consider completing the Bulk Apex Triggers unit on Trailhead. Deploying this trigger may block import operations due to hitting SOQL limits. – David Reed Apr 18 at 1:22
3

Before triggers only allow you to avoid DML for the records taking part in the trigger (i.e. in trigger.new and/or trigger.newMap).

Everything else still needs explicit DML.

| improve this answer | |
  • To clarify a little bit further, you need to edit the same in-memory instance of a record in trigger.new or trigger.newMap. – Derek F Apr 18 at 1:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy