Sunday, 30 May 2021

Invocable Methods in Apex

 The Invocable method will allow an Apex method to be executed as an invocable action.


Note :


Use the InvocableVariable annotation to identify variables used by invocable methods in custom classes.


The InvocableVariable annotation identifies a class variable used as an input or output parameter for an InvocableMethod method’s invocable action. 

If you create your own custom class to use as the input or output to an invocable method, you can annotate individual class member variables to make 

them available to the method.


ex :


public with sharing class InvocableApexExample {



  @InvocableMethod(label='Example Invocable Method' description='Example Invocable Method')

    public static List<ContactSearchResult> findRelatedContacts(List<ContactSearchRequest> inputParams) {

        List<SObject> inputs = inputParams[0].inputs;

        String firstRecordId = inputs[0].Id;

        String sObjectType = inputs[0].Id.getSobjectType().getDescribe().getName();

         String queryString = '';

        switch on sObjectType.toLowerCase() {

            when 'account' {

                queryString = 'SELECT Id, Name FROM Contact WHERE accountId = :firstRecordId WITH SECURITY_ENFORCED LIMIT 1';

            }

            when 'task' {

                Task task = (Task) inputs[0];

                String whoId = task.WhoId;

                queryString = 'SELECT Id, Name FROM Contact WHERE Id = :whoId WITH SECURITY_ENFORCED LIMIT 1';

            }

            when else {

                throw new InvocableMethodRecipesException(

                    'Unknown object type passed. This method only supports Account and Task.'

                );

            }

        }

        

         Contact contact = Database.query(queryString);

         List<ContactSearchResult> results = new List<ContactSearchResult>();

         results.add(new ContactSearchResult(contact));

        

        return results;

    }

  

  

  // This class Represents Input to the InvocableMethod 

 

 Public class ContactSearchRequest { 

    @InvocableVariable(label='Generic SObject records - Input' description='Input variable for generic Sobjects' required=true)

    Public List<SObject> inputs; 

 }

 

 // This class Represents output from the InvocableMethod

 

  public class ContactSearchResult {

        @InvocableVariable(label='Generic SObject record - Output' description='Output variable for generic SObject' required=true)        

        public SObject output;       

        public ContactSearchResult(SObject toOutput) {

            this.output = toOutput;

        }

    }

    

    // Internal custom exception class

 

   public class InvocableMethodRecipesException extends Exception {

   

     }

 

}



Implementation Notes :


1.The invocable method must be static and public or global, and its class must be an outer class.

2.Only one method in a class can have the InvocableMethod annotation.

3.Other annotations can’t be used with the InvocableMethod annotation.


Inputs and Outputs :


There can be at most one input parameter and its data type must be one of the following:


1.A list of a primitive data type or a list of lists of a primitive data type – the generic Object type is not supported.

2.A list of an sObject type or a list of lists of an sObject type.

3.A list of the generic sObject type (List<sObject>) or a list of lists of the generic sObject type (List<List<sObject>>).

4.A list of a user-defined type, containing variables of the supported types above or user-defined Apex types, with the InvocableVariable annotation. 

Create a custom global or public Apex class to implement your data type, and make sure that your class contains at least one member variable 

with the invocable variable annotation.


If the return type is not Null, the data type returned by the method must be one of the following:


1.A list of a primitive data type or a list of lists of a primitive data type – the generic Object type is not supported.

2.A list of an sObject type or a list of lists of an sObject type.

3.A list of the generic sObject type (List<sObject>) or a list of lists of the generic sObject type (List<List<sObject>>).

4.A list of a user-defined type, containing variables of the supported types above or user-defined Apex types, with the InvocableVariable annotation. 

Create a custom global or public Apex class to implement your data type, and make sure that your class contains at least one member variable 

with the invocable variable annotation.


Managed Packages :


1.You can use invocable methods in packages, but once you add an invocable method you can’t remove it from later versions of the package.

2.Public invocable methods can be referred to by flows and processes within the managed package.

3.Global invocable methods can be referred to anywhere in the subscriber org. Only global invocable methods appear in Flow Builder 

and Process Builder in the subscriber org.

No comments:

Post a Comment