Invocable Method in Apex Salesforce

by Rijwan Mohmmed
0 comment
Invocable-Method-in-Apex-Salesforce

Hello friends, today we will discuss the Invocable Method in Apex Salesforce. We use @InvocableMethod annotation on the apex method to invoke this method from the process builder. Without @InvocableMethod annotation, we can’t access the apex method in process builder.

Check this Wrapper class Datatable in LWC Salesforce

Invocable Method Snippets :

public class InvocableCtrl {
    @InvocableMethod(label='Update Contact' description='Update the list of contacts')
    public static void updateData(List<Id> contactIds) {
        
    }
}

Highlights Points :

  1. We can use only one @InvocableMethod annotation per class.
  2. The invocable method must be static and public or global, and its class must be an outer class
  3. Method should only one parameters
  4. We can’t use other annotations with the InvocableMethod annotation.

Let’s start with an example :

Step 1: Create an Apex class to update the account name when the Account is inserted.

public class InvocableCtrl {
    @InvocableMethod(label='Update Account' description='Update the list of accounts')
    public static void updateData(List<Id> accountIds) {
        List<Account> accList = new List<Account>();
        for(Account acc : [SELECT Id, Name FROM Account WHERE ID IN :accountIds]){
           Account ac = new Account();
           ac.Id = acc.Id;
           ac.Name = ac.Name + '-' + 'Household';
           accList.add(ac);
        }

        if(!accList.isEmpty()){
           update accList;
        }
    }
}

Step 2: Create a Process Builder, click Setup > Process Builder > New

Invocable-Method-in-Apex-Salesforce-Techdicer
InvocableMethod-in-Apex-Salesforce-Techdicer

Step 3: Add Object Account and Set execution when only when a record is created

Invocable-Method-in-Apex-Salesforce-Techdicer45

Step 4: Set Criteria, in my case I chose No criteria -just execute the action

Invocable-Method-in-Apex-Salesforce-Techdicer3
Invocable-Method-in-Apex-Salesforce-Techdicer3.png

Step 5: Select Action Type Apex, set Action Name, select Apex class method Invocable label, and in last set variables

Invocable-Method-in-Apex-Salesforce-Techdicer6

Now Activate the process builder and test the full process by creating an account record and checking account name with household postpend.

What’s your Reaction?
+1
0
+1
1
+1
0
+1
0
+1
1
+1
0

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.