You need to sign in to do that
Don't have an account?

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?
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
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.
Thanks Ankit. So this is the approach I'll need to take.