[SalesForce] Using a button to trigger a screenflow, which is followed by an Apex method

I'm in a situation where I have a Screen flow that is called when an action button is clicked on an opportunity, some data is input, and then that data is pulled into an Apex class.

I understand that a Screen flow can't be called from an Apex class (although I wish that would be changed) but is it at all possible to call an Apex class from a screen flow? The issue is that there is specific information that needs to be pulled in from the user first before the class is called, and I'm trying to figure out a way to do this without building out a fully custom visualforce page.

Currently, this is the process I'm hoping to achieve:

  1. User/Sales Rep clicks 'Void Contract' action on the Opp.
  2. User is taken to a screen flow where they enter the type of void they want to perform (SelectedVoidType), and a new total value for a sales rep's commission (newQR).
  3. My apex class is called after the screen flow finishes that receives those 2 new variables, and updates some records and some other miscellaneous actions from there.

Is this process possible with what I'm looking to do? (i.e. screen flow –> apex class) If so, I would love some clarification/guidance on how I can proceed with it. Thank you!

Best Answer

Calling Apex from a screen flow is easy to achieve. You simply have to make sure you can call the code via an @InvocableMethod. The approach I take is to have the invocable method in a separate class (what I call a "facade") that then invokes the required logic on my underlying class. This has a couple of benefits:

  1. You can adapt the input parameters to be more logically structured and
  2. You can have a number of different logic methods in one class that are related and that can themselves be invoked from flows etc. - any given class can only have one invocable method, so you simply have multiple facade classes that are used in the flows.

Parameters to (and outputs from) invocable methods are a bit messy, but you can do this using the related @InvocableVariable mechanism. Just make sure you remember these always take lists due to the way flows invoke methods.

Note that the invocation of the method is part of the screen flow, rather than after it. You can have the flow receive back data and then do different things, perhaps showing messages or even going and invoking different processing that might include additional screens in the flow.

Related Topic