I created my first Invocable Method and it is beautiful!

I created my first Invocable Method and it is beautiful!

My first experience on a project using Apex was when I was required to generate a random number using a flow. For those who are familiar with flows, you will know that there isn’t a function or action that can be used out of the box to generate a random number.

So the quickest solution was to write a random number generator in Apex. (See my previous two articles written about it here and here).

I ended up creating an Apex Invocable Action and it is the most beautiful thing there is! With my experience using actions and sub-flows in flows, I had a bit of experience with how a great invocable method should behave in a flow:

  1. Have descriptive input variables

  2. Have descriptive output variables

  3. Do data validation

  4. Let me know if something went wrong

For generating a random number, the only data validation necessary was:

  1. Ensure the lower value isn’t bigger than the upper value

  2. Ensure that both values provided aren’t smaller than zero

These validations can be contained inside their method and can use an if-statement to check the above conditions and return a boolean flag if there are any issues.

The nice thing about having it in its method is that it can always be expanded later if any other required validations become apparent.

private static Boolean validateInput(Integer lowerValue, Integer upperValue){
        Boolean errorFlag;
        if(lowerValue > upperValue){
            errorFlag = true;
        }
        else if(lowerValue < 0 || upperValue < 0){
            errorFlag = true;
        }
        else{
            errorFlag = false;
        }
        return errorFlag;
    }

Now it is time to create the invocable method. Remember to add @InvocableMethod before the method!

@InvocableMethod(Label ='Generates a random number' Description='Generates a random number' Category='Event')
    public static List<Results> randomNumbers(List<Requests> requests){

        List<Results> results = new List<Results>();
        Boolean invalidInput;
        for (Requests request:requests){      
            invalidInput = validateInput(request.lowerValue, request.upperValue);
            Results result = new Results();
            if (invalidInput == false){
                Integer randomNumberValue = RandomNumberGenerator.generateRandomNumber(request.lowerValue,request.upperValue);
                result.randomNumber=randomNumberValue;
                result.errorFlag=invalidInput;
            }
            else{
                result.randomNumber=null;
                result.errorFlag=invalidInput;
            }
            results.add(result);
        }
        return results;
    }

Can you see the validation check done first? You get null if you give it incorrect data 🙂

When the method is tagged as an Invocable Method it will become available for use in a flow. I created a quick screen flow that can be used to test the Invocable Method:

  1. Assign the upper and lower values to variables that can be used in merged fields in the screen elements

  2. Call the Invocable Method and provide it with the lower and upper values

  3. Check to see if the Invocable Method flagged anything with regard to the input values (errorFlag equals false/true)

  4. Depending on the decision outcome:

    1. If no error is raised, display the randomly generated number on the screen

    2. If an error is raised, notify the user with an error message on the screen

Here is a GIF as well!