[SalesForce] Validation Rule To Allow Only One Checked Record

I have a custom object Object_X, which has a checkBox field Activate__c. My requirement is that only one Object_X type record can have the Activate__c checked at any given time. So if Object_X already has a record with Activate__c checked, another record with Activate__c checked should not be allowed to be created.

I already have a trigger for this but I'm wondering if there is a way this can be easily achieved by a validation rule.

Best Answer

Disclaimer: Trigger may be the best route for the given situation.

AFAIK You cannot iterate over a set of records using validation rule.Can the activate flag be edited by anyone in the system? If so I would stick to trigger approach, else I can think of a crazy solution which might be an alternative to code approach but more maintenance for the admin every time the flag is changed on the object X.

Step1: Create a custom label which holds the Id of the opportunity where activate = true.

Step2 : Create a validation rule to check both new opptys and updates to existing oppty.

OR(
    AND(
        ISNEW(),
        Activate__c = true
    ), 
    AND( 
        Id != $Label.custom_label_name,
        ISCHANGED(Activate__c ),
        Activate__c = true 
    )
)