Validation rule didn't fire for some records
|Knowledge Article Number||000199651|
Some customers will often report they found records that seem to have bypassed validation rules because custom validation rules should have prevented the record to be created/updated in such state. For example:
Validation Rule Criteria:
Name = "test"
With this criteria in place, you wouldn't expect to see any records with "test" as the name. The issue occurs when you do find records with the name "test".
There are three possible reasons this can happen:
The validation rule was edited before the record was created and updated again after the record was created.
a- Validation rule criteria: Name = "test"
b- Validation rule criteria is updated to: Name = "test2"
c- A record is created with the name "test"
d- Validation rule criteria is updated again to: Name = "test".
This will give the impression that the record bypassed the validation rule, but the key here is that the record didn't meet the validation rule criteria when it was created. In order to confirm this theory, you can check the Setup Audit Trail, and there you will find if the validation rule criteria was indeed edited.
The field in question is being updated by a workflow rule/approval process field update.
It's possible that the field being evaluated by the validation rule, is also updated by a field update. Field updates are executed after validation rules as documented here Triggers and Order of Execution
The following scenario can happen:
a- Record is created/updated
b- Validation rule is evaluated - The record passes the validation rule
c- The field update is triggered and updates the field in question (the field is updated to the value evaluated by the validation rule)
d- The validation rule is not evaluated again - Hence we don't prevent the record from being created/updated even though it does not pass the validation rule criteria
The workaround for this particular scenario, is to write an Apex Trigger that acts as a validation rule using the addError method. The benefit is you can control when the trigger (acting as a validation rule) is evaluated. In this case, you can create a BEFORE UPDATE or AFTER UPDATE trigger so that it is evaluated after the field update.
The record may have been created using Quick Create
Validation rules are not evaluated when a record is created using the Quick Create feature. See Creating Records with Quick Create