2

Question: How are errors (validation rules, addError in triggers etc.) handled by time-based workflows? And are there any best practices regarding time-based workflow error handling?

Example: In my example I'm trying to set up a time-based workflow that updates the status of contracts based on their end date. This may, however, conflict with certain validation rules. I'm wondering whether it will update the status irregardless of the error and whether it will somehow notify me & the user.

3

** significantly updated my answer as it contained a wrong assumption **

Validation rules will not run after a field update (credits to Amit. See his answer for a full explanation).

So you have to do the validations before scheduling the update. Do note that on every new update of the record, the workflow rules are evaluated again and if the conditions are no longer met, the scheduled field update is revoked. So theoretically the update will then never happen if the record does not meet the conditions. However, this is not valid if the conditions consist of data on related records.

Validations in apex triggers WILL run after a field update, so they will block the update and leave the record unchanged. In this case you can also build in a notification mechanism that the update failed, e.g. send out an email or insert a record into a logging table.

All in all the safest way to do this would be via an apex trigger, including a notification mechanism.

FYI some earlier discussion on this subject here: Bests practices for handling potential errors as a result of time based workflow

  • I dont agree with the 1st statement. Record is edited as Validation Rules are not checked by W/F field updates. – Amit Bangad Jan 5 '15 at 10:10
  • Good point Amit. I didn't know that. Will check and get back.. – Guy Clairbois Jan 5 '15 at 10:26
  • I updated my answer since indeed it contained a wrong assumption. Thanks for pointing it out. – Guy Clairbois Jan 5 '15 at 12:15
4

My understanding says that Workflow field updates will not fire custom validation rules. The field will be updated irrespective of validation rule. Before & After triggers will fire. SO AddError might work but validation rules are bypassed.

Here is a extract to second my view "Trigger and Order of Execution" point number 13 :

If the record was updated with workflow field updates, fires before update triggers and after update triggers one more time (and only one more time), in addition to standard validations. Custom validation rules are not run again.

Also I tried POC to cross validate and it works as mentioned. So that's the power of workflow rules - Because updates to records based on workflow rules don’t trigger validation rules, workflow rules can invalidate previously valid fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.