1

I have object called Test with fields: name and checkbox.

When I add first record, I set the checkbox of this record to true. Next, I add second record.

Test__c Tab "All" List View

If the checkbox in the second record will be changed to true I want to change the first record checkbox to false so I write a apex trigger to do that:

trigger checkUpdate on Test__c (before update) {
        List<Test__c> listTest = [SELECT id, name, checkbox__c FROM Test__c where checkbox__c = true ];
        for(integer i = 0 ; i < listTest.size(); i++){
            listTest[i].checkbox__c = false;
        }
        update listTest;
}

but after edit the second record trigger was fired and I get that exception:

Error Message

It caused by fact that DML call on records that are in the trigger. But how do I properly implement this functionality?

4

Implement this functionality declaratively, via Workflow Rule. A solid axiom of Salesforce development could be framed:

Whenever you have a choice between declarative and programmatic functionality, choose the declarative option.

Typical exceptions to this rule would be:

  • Performance - functionality needs to be optimized
  • Error Handling - declarative functionality does not provide optimal user experience

I'm sure there are others as well, the key is to make sure you only choose the programmatic option (here Apex Trigger) if you have a compelling reason to do so. Try to write as little code as possible.


If you are trying to make it so only one record can have this box checked at a time, you may indeed need Apex. However, note the documentation on Triggers states:

There are two types of triggers:

  • Before triggers are used to update or validate record values before they’re saved to the database.
  • After triggers are used to access field values that are set by the system (such as a record's Id or LastModifiedDate field), and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue. The records that fire the after trigger are read-only.

So you need to move your logic to an after trigger. It seems like you should also trigger after insert, but your question doesn't make your requirements very clear.

  • Thanks for tip. Second question - there is a possibility that Workflow Rule will for the first record in the case when the object is empty set the checkbox to true? – Adamo Jul 26 '16 at 15:52
  • I'm having a hard time with your grammar. I have no idea what you're asking. – Adrian Larson Jul 26 '16 at 15:53
  • Sorry. I asked for Workflow Rule. If the Workflow Rule handle me case that the object is empty and I want to always set to first record checkbox to true. Only when I add the first record to the object. – Adamo Jul 26 '16 at 15:56
  • @AdrianLarson I think he is saying that he wants to know if Workflow Rules can handle the condition where if the OBJECT has no records, he wants the first record inserted into that object to mark his Boolean to True – Morgan Marchese Jul 26 '16 at 16:09
  • 1
    @Morgan Right, if that is the requirement it must be trigger, it just needs to use after events. I added that interpretation to my answer. – Adrian Larson Jul 26 '16 at 16:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.