how to create apex class in salesforce
Every time Salesforce surprises us with new additions to its list of out-of-box features. One of the out-of-box features is the ability to invoke an apex method from the Process builder. There are some scenarios where developers would need to process a chunk of data in an apex class when a certain event happens. For instance, If your business case depends on an action occurring due to an event, the developer can use the process builder to automate the process and call an apex code with complex logic from the process builder. In this article I will explain the steps involved in calling an Apex method from Process builder.
Introduction to Salesforce Process Builder
Salesforce.com's process builder is a powerful automation tool, mostly used by the administrators, power users, and developers to automate a process. Process builder also provides a visual representation of the process flow as a developer builds it. It is a set of "if, else and when" statements accumulated together to customize the automation. Every process that is being built using the Process builder consists of three parts. They are
- Triggers
- At least one filter criteria node, and
- At least one action.
Process Builder allows us to set triggers for tasks with multiple filtering criteria nodes, the developer can configure immediate actions or schedule actions to be executed at a specific time.
Steps for calling Apex from Process Builder
Calling apex class from a Process builder is needed when complex logic has to be bundled in the form of an apex class and has to be automated as part of a process flow.
To invoke an apex method you can use @InvocableMethod and follow the following steps:
- Invocable methods can have only one parameter. These methods can accept only a list of primitive data types or list-of-lists of the primitive data types.
- For example, the developer can have List<Id> or List<String>
- In any apex class, there can be only one Invocable method.
Syntax to call Invocable method
public class AutoConvertLeads {
@InvocableMethod
public static void Testmethod(Integer LeadIds)
{ code…………………… } }
Sample Use Case or Scenario
Assume a business case when a user has to create a number of contacts for an account. Users normally have to click many fields to create the number of contacts. One form of automation expected by the client is to add a field 'Number of Contacts' while creating an account. Based on this number, the client expects to create 'N' basic contact records automatically. Users will later fill in or update the appropriate field in the contacts. This is expected to save a substantial amount of time and improve user adoption as well.
This example below will explain how to create the apex class and Process builder to " Insert 'n' number of contacts based on the field 'Number of Contacts' while account record is created." Using these steps you can understand about calling apex from Process builder flows.
1. Create a custom field
- Create a new custom field "Number of Contacts" in the account object (Data Type: Number).
2. Create an apex class
Apex is a strongly typed object-oriented programming language specifically for the Force.com platform.
To create a class in Salesforce:
- Go to Setup -> Build -> Develop -> Apex class or by using developer console go to File -> New -> Apex class
- Click on the new button and create the class 'CreateChildRecords'. This will create
- Write a class with an invocable method as defined above.
Apex class code using the invocable method to create contacts for accounts
/**
Class Name: createchildRecords
Purpose: Create N number of contacts based on the field 'Number of contacts' while Account is created.
**/
public class CreateChildRecords {
@InvocableMethod (label='Create Contacts')
public Static void createContact(List<Id> accIds){
List<Contact> conListToInsert = new List<Contact>(); //list to collect and insert the contacts
//Query the accounts
List<Account> accList = [Select Id, Name, Number_of_Contacts__c from Account where Id =:accIds];
//loop through the accounts and create contacts
for(Account acc : accList)
{
if(acc.Number_of_Contacts__c != null)
{
for(integer i=1;i<=acc.Number_of_Contacts__c;i++)
{
Contact con = new Contact();
con.LastName = 'Invocable' + acc.Name +' '+ i;
con.AccountId = acc.Id;
conListToInsert.add(con);
} } }
if(!conListToInsert.isEmpty()) {
insert conListToInsert; } } }
3. Create the flow using a process builder
Salesforce process builder is a powerful tool which you can use to automate business processes. It has a simple interface that allows you to point-and-click to select objects and fields while setting up immediate and time-based actions.
- From Setup, enter Process builder in the quick find box, click Process builder, and then click new.
- Name the process (say create contacts). The API name gets updated automatically. Select "Record changes" and click 'Save'.
- Click "+" and add an object to associate your process with an object. For this, you can choose the account object and bind the process to the account object.
- Select "only when a record is created" and click 'Save'.
- Enter the condition to check if the ' Number of Contacts ' is greater than 0. If the user enters the number of contacts '0' the code will not execute.
- Enter criteria name "Number of Contacts"
- Criteria for executing actions – select "Conditions are met"
- Set conditions: Field – Enter "Account.Number_of_Contacts__c" ; Operator – Select "Greater than" ; Type – Select "Number" ; Value – Enter "0"
Define criteria based on the condition
4. Next, Add actions to execute when the criteria are met
- Under immediate actions, click Add action.
- Select Apex class in action type.
- Enter the name in the action name.
- The classes with the invocable method are available in the apex class field. Select the desired class.
- Afterward, you can also assign parameters to the method. For this click on the add row in the set apex section.
- Select the field of the apex method.
- Set type of value. (You can select between formula, global constant, field reference, and id).
- Enter value and Click save.
Add the action with action type apex
5. Test the Results of calling apex from Process builder
- Navigate to the Account object, create a new account and enter 4 in the 'Number of Contacts'.
- Save the account.
- Query the contacts object.
- There will be four contacts created for the Account with basic details.
Result of process builder apex invocation
Similarly, try with different numbers in the number of contacts when you create a new account. The process builder and the class will take care of creating contacts.
The above was a simple example. In short, this feature to Invoke an Apex Class from Process builder will come in handy when your requirement involves complex logic present in an apex method that needs to be automated using the Process Builder.
Please check out other salesforce.com blogs from my team at http://bestirtech.com/blog/category/salesforce.com/ and share your comments.
Post Views: 11,351
how to create apex class in salesforce
Source: http://bestirtech.com/blog/2020/07/calling-apex-method-from-process-builder/
Posted by: najerawitand.blogspot.com
0 Response to "how to create apex class in salesforce"
Post a Comment