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
imrohitimrohit 

Need help in validation rule

I have one object which has one field of checkbox type now my requirement is that at a time only one record in the object can have checkbox value as true so suppose If I save a record with checkbox value as true and then I create another record and trying to save record with checkbox as true so in this case validation rule should throw error.

thanks in advance

Abdul KhatriAbdul Khatri
Your requirement is very strange.

Are you saying that in the Object, that field should only be true for one record from the entire Data of that Object. I hope this must be a configuration object and not transactional otherwise the data in that object will grow.

Anyway based on what information you provided and what I understood you can achieve that using the trigger.

I have never done trigger like this before. I truely believe some information is missing. I have used dummy names you can replace them or change them as per your need.
 
trigger UpdateCheckbox on SObject (before insert, before update) {

	sObject obj = [SELECT Id FROM SObject WHERE CheckboxField = true];
    
    if(obj != null)
        Trigger.new.get(0).addError ('There is already a record');
    
}

 
imrohitimrohit

Hi Abdul 

Thank you, You gave your valuable time for my problem, Yes you are right that It can be easily done by trigger but I was thinking that Is it possible to solve the problem using the validation rule. I just want to know that Is it possible in validation rule that you can check the values of  the fields of the existing records while creating the new record

Abdul KhatriAbdul Khatri
No it is not possible through Validation Rule because you need to run the SOQL to see through the database if any record as that value true. Validation Rule do not support SOQL or you cannot run through database.