5

I am using a Record-Trigerred Flow to verify an input by the user. If the input is outside of a certain range I need to display an error on the page.

I have a decision that verifies the value. However I can't figure out if there is something like SObject.addError() in a Record-Trigerred Flow?

3

3 Answers 3

1

Starting from Winter 24, Salesforce has added the capability to add Custom Error interaction component in a record triggered flow as detailed below

Create Custom Error Messages in Record-Triggered Flows Instructions

The instructions can be found in the release notes section too https://resources.docs.salesforce.com/246/latest/en-us/sfdc/pdf/salesforce_winter24_release_notes.pdf

And the idea delivery confirmation can be seen in the following link https://ideas.salesforce.com/s/idea/a0B8W00000H7QoLUAV/allow-customization-of-error-messages-for-recordtriggered-flows

2

For Before Save flows, you need to rely on the Order of Execution to do record validation. This means focusing on steps 3-5:

  1. Executes record-triggered flows that are configured to run before the record is saved.

  2. Executes all before triggers.

  3. Runs most system validation steps again, such as verifying that all required fields have a non-null value, and runs any user-defined validation rules. The only system validation that Salesforce doesn't run a second time (when the request comes from a standard UI edit page) is the enforcement of layout-specific rules.

Create a custom checkbox field on the object Is_Amount_Out_of_Range__c and set to true in a Flow Assignment element when the input is out of range. Then code a validation rule that tests for Is_Amount_Out_of_Range__c being true with an appropriate error message.

The only reason to do the above is if the error condition requires testing against other objects that the Flow is fetching that would be otherwise unavailable in a normal Validation Rule

1

Google's Mitch Spano has an excellent trigger framework, which includes (among other things) an invocable Apex method for adding an error to an SObject from a Flow:

TriggerActionFlowAddError.cls

@SuppressWarnings('PMD.ApexDoc')
public inherited sharing class TriggerActionFlowAddError {
    @InvocableMethod(
        category='Trigger Action Flow'
        label='Add Error'
        description='Adds an error to an sObject'
    )
    public static void addError(List<Request> requests) {
        for (Request myRequest : requests) {
            if (!String.isBlank(myRequest.fieldName)) {
                myRequest.record.addError(myRequest.fieldName, myRequest.errorMessage);
            } else {
                myRequest.record.addError(myRequest.errorMessage);
            }
        }
    }

    public class Request {
        @InvocableVariable(
            label='Record'
            description='Record to add the error to'
            required=true
        )
        public SObject record;
        @InvocableVariable(
            label='Field Name'
            description='API name of corresponding field'
        )
        public String fieldName;
        @InvocableVariable(
            label='Error Message'
            description='Error Message'
            required=true
        )
        public String errorMessage;
    }
}

Record-triggered flows may be able to call an Apex method like the one above in the after update context (when "optimized for actions and related records"). In the before update context (when "optimized for fast field updates"), unfortunately you can't call an Apex action. This is too bad, since it's more efficient to throw an error in the before update context, as Salesforce themselves has noted:

Users experience less of a delay in response time if errors are added to before triggers.

Of course, Mitch's trigger framework gets around this problem by invoking auto-launched flows from the Apex framework itself, which can be done in the before update context, and which avoids record-triggered flows entirely.

1
  • Thanks for the valuable comment!
    – Arthlete
    Commented Apr 13, 2023 at 0:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .