3

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 at line 17 column 9

Thank you,

Justin

1 Answer 1

4

It's receiving a list, so you have to iterate over the variable list:

for(VarList variable: variables) {
    variable.opp.addError(variables.errorMessage);
}

You shouldn't assume that the list will only have a size of 1, so it's important to iterate over all of them.

3
  • Thanks for the help. I changed the last "variables" to "variable" and then it no longer showed the error message. Now to test and make sure everything runs as it should. Thanks again.
    – justinmcp
    Commented Oct 7, 2015 at 21:49
  • Unfortunately, it doesn't throw an error on the page, like I would expect it to. I run a process to kick off when certain fields are changed. Then, a flow runs from the process to do some further analysis of how fields should be updated. If there is a validation error, I am catching them with decisions in the flow. Then, I am running apex code to display the error. I was hoping the posted code would work, but it does not. Any ideas?
    – justinmcp
    Commented Oct 7, 2015 at 22:02
  • @sfdcfox Years later, wondering if you have any wacky inspiration on this. I created this idea: ideas.salesforce.com/s/idea/a0B8W00000HmIQdUAN/… Getting the sObjectType is easy enough. I just pass that as an extra param in the flow. Or you can be nuts and serialize the object, 'cause the object type is in there. But my remaining issue is calling addError(..). Chris Peterson wasn't surprised when I brought it up to him and he suggested posting the Idea.
    – David
    Commented Mar 17, 2022 at 21:02

You must log in to answer this question.

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