+ Start a Discussion
sandeep reddy 37sandeep reddy 37 

flow with apex class

can any one please tell me how to connect apex class to flow process give some navigtion inflow
NagendraNagendra (Salesforce Developers) 
Hi sandeep reddy 37,

Please find the internal navigation how the apex class gets connected to a process flow.

Process.Plugin is a built-in interface that allows you to process data within your organization and pass it to a specified flow. 

The interface exposes Apex as a service, which accepts input values and returns output back to the flow.


When you define an Apex class that implements the Process.Plugin interface in your organization, the Cloud Flow Designer displays the Apex class in the Palette.

Process.Plugin has these top-level classes.
  • Process.PluginRequest passes input parameters from the class that implements the interface to the flow.
  • Process.PluginResult returns output parameters from the class that implements the interface to the flow.
  • Process.PluginDescribeResult passes input parameters from a flow to the class that implements the interface. This class determines the input parameters and output parameters needed by the Process.PluginResult plug-in.
Sometimes your flow needs to do more than the default elements allow. In that case, call an Apex class from your flow by using one of two flow elements: Apex Plug-in and Call Apex.

Basically, we have two options while making an apex class available for a flow.
  •  Process.Plugin Interface
  • @InvocableMethod Annotation
Its recommend using the @InvocableMethod annotation instead of the Process.Plugin interface because Process.Plugin interface supports customizing how the class appears in the palette, the @InvocableMethodannotation provides more functionality. The following table describes the features supported by each option.

To illustrate the difference between these two implementation methods, here are two classes that do the same thing: get an account name from a flow and return that account’s ID.

This class implements the @InvocableMethod annotation.
global class lookUpAccountAnnotation {
   @InvocableMethod
   public static List<String> getAccountIds(List<String> names) {
      List<Id> accountIds = new List<Id>();
      List<Account> accounts = [SELECT Id FROM Account WHERE Name in :names];
      for (Account account : accounts) {
         accountIds.add(account.Id);
      }
      return accountIds;
   }
}

This class implements the Process.Plugin interface
global class lookUpAccountPlugin implements Process.Plugin { 

   global Process.PluginResult invoke(Process.PluginRequest request) { 
      String name = (String) request.inputParameters.get('name');
      Account account = [SELECT Id FROM Account WHERE Name = :name LIMIT 1][0];

      Map<String,Object> result = new Map<String,Object>();
      result.put('accountId', account.Id);
      return new Process.PluginResult(result);
   }

   global Process.PluginDescribeResult describe() { 
      Process.PluginDescribeResult result = new Process.PluginDescribeResult(); 
      result.Name = 'Look Up Account ID By Name';
      result.Tag = 'Account Classes';
      result.inputParameters = new 
         List<Process.PluginDescribeResult.InputParameter>{ 
            new Process.PluginDescribeResult.InputParameter('name', 
            Process.PluginDescribeResult.ParameterType.STRING, true) 
         }; 
      result.outputParameters = new 
         List<Process.PluginDescribeResult.OutputParameter>{              
            new Process.PluginDescribeResult.OutputParameter('accountId', 
            Process.PluginDescribeResult.ParameterType.STRING)
                }; 
      return result; 
   }
}

Notice : lookupAccountAnnotation is less than half the length (11 lines) of lookupAccountPlugin (28 lines). In addition, because the annotation supports bulk operations, lookupAccountAnnotation performs one query per batch of interviews.lookupAccountPlugin performs one query per interview.

Please mark my solution as the best answer if it helps you.

Best Regards,
Nagendra.P
Frédéric ProvotFrédéric Provot
Thank you Nagendra,
I'm trying to use also the @InvocableMethod annotation from a visual flow. The flow sees the exposed method, but it's not clear how I can process the result.
As only one method can be exposed in a class, I would have liked to get a Map as a result, which is not possible.

How could we obtain and handle
  • a single object as a result
  • a list of objects
  • more than one result?
Examples in forums on the web only describe void methods, or don't show the flow details.
Best regards
Frederic