You need to sign in to do that
Don't have an account?
Scott.M
Complex Validation Rules
Hi,
Is it possible to throw a new System.DmlException from a trigger. I need to create a complex validation rule that can't be done with the normally defined custom validation rules.
Thanks for any help!
Message Edited by Scott.M on 09-17-2008 07:41 AM
Is it possible to throw a new System.DmlException from a trigger. I need to create a complex validation rule that can't be done with the normally defined custom validation rules.
Thanks for any help!
Message Edited by Scott.M on 09-17-2008 07:41 AM
trigger validateContract on Contract (before insert) {
public class CustomDmlException extends Exception {}
for(Contract c: Trigger.new) {
Contract[] contracts = [SELECT id FROM Contract WHERE CustomObject__c = :c.CustomObject__c AND ((StartDate >= :c.StartDate AND StartDate <= :c.EndDate) OR (EndDate >=: c.StartDate AND EndDate <=: c.EndDate))];
if(contracts.size() > 0) {
throw new CustomDmlException('The selected custom object has a conflict in the given date range');
}
}
}
Thanks!
1. Create a hidden field on the object
2. Create an on before update and insert trigger that executes the validation and sets the hidden field to some value
3. Write a validation rule that checks if the hidden field and displays the desired message on some condition
That's it, now you can get as funky with your validation as you like :)
Message Edited by Scott.M on 09-17-2008 07:41 AM
Kind of like a guy with an arm under a rock, shouting for help, then resorting to chewing his own arm of, then writing books on best approach for it.