Salesforce – Resolve Validation Rule Conflicts with Flow

validation-rulevisual-workflow

I am having trouble with a new Validation rule because it is conflicting with Flow. I am getting this error:

Error Occurred: The flow tried to update these records: 0064V00001LmbdtQAB. This error occurred: FIELD_CUSTOM_VALIDATION_EXCEPTION: Closed Opportunities cannot be edited. Please create a new Opportunity.. You can look up ExceptionCode values in the SOAP API Developer Guide.

Salesforce Error ID: 2064168760-169264 (-617785307)

Need: We want to prevent the editing of Closed Opportunities except for certain users through a Validation Rule.

Problem: My validation rule seems to be conflicting with a flow we have that changed the Close Date to Today() when an Opportunity is Closed Won/Lost

Here is the formula for my Opportunity Validation Rule

AND(

PRIORVALUE(IsClosed)=True,

$User.Alias<>"bbond",

$User.Alias<>"cburn",

$User.Alias<>"igend",

$User.Alias<>"jgord"
)

Here is the criteria for the Opportunity Flow. It updated the Close Date to Today when an Opportunity is Closed:

1- Trigger when a Record is Created or Updated

2- Condition Requirements – Formula Evaluates to True

3- Formula: AND(
ISCHANGED({!$Record.StageName}),
{!$Record.IsClosed}=TRUE
)

4- enter image description here

Does anyone know what I am doing wrong?

Thank you!

Best Answer

From my comment, the primary issue here is using an after-save record triggered flow (or flows) rather than a before-save record triggered flow.

tl;dr: If you're going to update a field on the record triggering the flow, use before-save flows.

Flow behavior

With an after-save flow, field updates cause the record to go through the "save procedure" again. This means that your validation rules are going to be evaluated again, and you're running into an issue when they are.

This is, as far as I've seen, the main difference between field updates on a workflow (which is being sunset), and field updates from an after-save flow. With workflow rules, the custom defined validation rules were not evaluated on the next run through the "save procedure".

So if validation rules are going to cause a problem, you should be using before-save flows (which are executed before validation rules are evaluated). Another benefit to using before-save flows is that they don't cause the record to go through an additional "save procedure" (saving cpu time, clock time, queries, query rows, dml rows, and dml statements).

The one gotcha

If your record triggered flow runs every time (no conditions to meet for the flow/field updates to run), then any change will attempt to update the close date.

This should trip your validation rule, but the error message your users get will probably be confusing. It could make them think that the flow is the problem, it could report the CloseDate field as the problem (even if your users were modifying a different field).

Good practice here would probably be to adjust your flow so it only fires when the Opportunity is changing to a closed won/lost state.

Related Topic