5

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.

  • who can edit the activate flag, and how many times do you think the activate flag on the record needs to be changed? – Rao Jun 18 '15 at 22:45
  • Anyone who has access to the Object_x can edit the Activate flag. There is no limit of number of times the flag can change, only that if the flag of one record changes I need to check all other Object_X records in the table to see if there is another record before allowing to save it – navD87 Jun 18 '15 at 22:52
4

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 
    )
) 
| improve this answer | |
5

One other validation rule approach would work if the Object_x is a detail in a master detail relationship. You could count the number of detail records that have the Activate__c = true. Then create a validation rule to fire when the count is greater than one.

| improve this answer | |
2

If you want to ensure only one active record in your entire org, I would use a trigger that uses a service class like below:

public without sharing class ObjectServices
{ // This should be enforced even if someone doesn't have the right visibility
    public static Boolean hasActiveObject
    {
        get
        {
            if (hasActiveObject == null)
            {
                hasActiveObject = [
                    SELECT count() FROM Object_X__c WHERE Activate__c = true
                ] > 0;
            }
            return hasActiveObject;
        }
        private set;
    }

    public static void validateActiveObjects(List<Object_X__c> records)
    {
        if (!hasActiveObject) return;
        for (Object_X__c record : records)
        {
            if (record.Activate__c)
                record.addError(Label.Only_One_Active_Object);
        }
    }
}
| improve this answer | |
1

You can check like this

    List<Object__x> objList = [SELECT ID,Activate__c WHERE Activate__c = true];
if(objList.size()>0){
   here you can insert new record in Object__x
}
| improve this answer | |

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.