function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RayAllenRayAllen 

Apex Class to Handle Flow/Process Errors from Validation Rules (Invocable Method/Variables)

Hi,

I'm working on converting our workflow rules to Processes and Flows so that they can be better consolidated and followed by the business.  I am running into the "Unhandled Error" message due to validation rules that are in place.  So, I'd like to determine the "errors" in my flows, and then pass the information into an apex class that will display the message that I use in my validation rules.

I feel as though I am close, but I'm unsure of how to access the invocable variables within the invocable method.  I get an error message on the last line of code when compiling, and am not sure how to accomplish my desired result.  I am pretty new to apex, so any guidance would be much appreciated.  Code and error message below.

Apex Class
public class OpportunityErrorHandling{
   
    // Get variables from flow/process
    public class varList {
        @InvocableVariable(label='Error Message' required=true)
        public String errorMessage;
        @InvocableVariable(label='Opportunity' required=true)
        public Opportunity opp;
    }
   
    // Add error
    @InvocableMethod(
        label='Adds an error to the record'
        description='Will display an error message to the page instead of the unhandled exception error')
    public static void addErrorMessage(List<varList> variables) {
        // Present error message to user
        variables.opp.addError(variables.errorMessage);
    }
}

Error Message
Error: Compile Error: Initial term of field expression must be a concrete SObject: List<OpportunityErrorHandling.varList> at line 17 column 9

Thank you,

Justin
KMForceKMForce
Hi Justin,
     last line should be 
for(VarList var : variables){
     var.opp.addError(var.errorMessage);
}

Even if you get this compiled, I would like to know how you are planning to pass errors and reference to opportunity.
As per process builder UI only formulae,reference to fields and static values can be passed. 
The problem you are trying to solve looks interesting.!!
Thanks,
KMForce
RayAllenRayAllen
Hi KMForce,

Thank you for the update.  I figured that out previously, but unfortunately I didn't get the desired result, so I've somewhat given up on the task in order to focus on others.

I have a process that runs when certain fields are changed on an opportunity.  This process then calls a flow that makes more intricate decisions based on other fields on the opportunity.  There are a few situations where a workflow rule prevents the record from being saved and I get the "Unhandled Error" from SF. I'd rather throw a detailed error so I put the above apex class into the flow at the point of the "error" rather than having the flow try to update the record with the current values. The "InvocableMethod" part allows me to pass values from the flow into the apex class, but unfortunately it doesn't produce an error message on the opportunity.

Thinking about it now I might be able to get to the end result that I want by updating a particular field with the error message during the flow and then have a trigger run, if that field is populated, that displays the information the field.  Sadly, I've been given several other projects so this has been put on the backburner, but I'd like to find a solution since this is a pretty common problem that hasn't been addressed yet by SF.

Thanks for your help,

Best regards,

Justin