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
calvin_nrcalvin_nr 

Validation rule which references other records?

I am trying to create a validation rule to do the following.

 

When creating a record which has a check field called flag, check if any other record of the same sObject type has its flag checked. If yes do not save.....

 

Since I need to reference other records, how can this be done in a validation rule. is this even possible?

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,


I don’t think you can achieve this through the Validation Rule. But you can achieve this through the trigger. Try the below code as reference:
Trigger TestFalg on CustomObject__c(before insert)
{
List<CustomObject__c> c=[select id from CustomObject__c where flag__c=true];
If(c.size() > 0)
{
// Write your error message by using adderror method.
}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,


I don’t think you can achieve this through the Validation Rule. But you can achieve this through the trigger. Try the below code as reference:
Trigger TestFalg on CustomObject__c(before insert)
{
List<CustomObject__c> c=[select id from CustomObject__c where flag__c=true];
If(c.size() > 0)
{
// Write your error message by using adderror method.
}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
calvin_nrcalvin_nr

Thanks Ankit. So this is the approach I'll need to take.