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
sohail najmisohail najmi 

Validation Rule on checkbox

Required Scenario:
-User add one record with [name, address, favorite(checkbox[checked])].
-User can add many record without favorite[checked]
-On adding new record if user apply favorite[checked] and save, user will be prompt with error message, you already marked favorite a record.

I have applied validation rule: IF( Set_Primary__c ,(TRUE), (Set_Primary__c) )
following rule are working if there is already a record with favorite[ checked] . means not allowing to add for 2nd time.

When i am adding data for first time, error message show, not allowing me to add data. Need to know what iam missing ??

Thanks.



 
VinayVinay (Salesforce Developers) 
Try using NOT(ISNEW()) so that validation won't execute for new records.

https://blog.bessereau.eu/assets/pdfs/salesforce_useful_validation_formulas.pdf

Hope this helps...

Thanks,
sohail najmisohail najmi

Not working for me -> NOT(ISNEW()). This is restricting the record in same form. i.e. if i have added data with a favorite[checked], on edit if i uncheck the field and save it, it is showing me error. I can add another data with favorite[checked].

i want only 1 favorite[check] data in my listing. 

 Need to know what iam missing ??

Thanks.

ravi soniravi soni
hi sohail,
I think you should try following trigger.
trigger fetchAccFavoriteRec on Account (before insert,before update) {
    if(trigger.isBefore){
        boolean hasFavorite = false;
        for(Account Acc : [SELECT Id,Name,favorite__c From Account WHERE favorite__c = true limit 1]){
            hasFavorite = true;
        }
        if(trigger.isInsert || trigger.isUpdate){
            for(Account acc : trigger.new){
                if(acc.favorite__c){
                    if(hasFavorite){
                    acc.AddError ('you already marked favorite a record.');    
                    }
                     
                }
            }
        }
    }

}

try it and let me know if it helps you.
if it is helpfull for you , marking it as best.
Thank you