Professional Documents
Culture Documents
Invocable Methods
Invocable Methods
==================
To invoke the Apex Class Method from the Process Builder, we have to use the
"InvocableMethod" feature. We have to define the required method by using an
annotation called as "@InvocableMethod()".
Rules:
------
1. The Invocable Method should be always defined with "Static" keyword.
5. We can Supply the Primitive Values, SObject Type Values, Collection Type
of values to the InvocableMethod.
Syntax:
Public Class <ClassName>
{
@InvocableMethod(Label='Reference Name for Method' Description =
'Comments')
Public static void <MethodName>(<Parameters>)
{
// Write the Business Logic..
}
}
Ex:
Public Class AccountsManager
{
@InvocableMethod(Label='Create New Accounts' Description='This Method
is
used to Create New
Account Records')
Public static void CreateAccounts()
{
// Write the Code to Insert New Accounts.
}
}
UseCase:
========
Create a Process on the Hiring Manager Object, To Create a Related Position
Record inside the Position Object.
1. Write the Business Logic inside the Class and the Method.
2. Invoke the Method from the Process Builder.
3. Upon Invoking the Method from the Process Builder, Supply the Newly
Created Hiring Manager Record, to Create the Related Position.
Class Code:
-----------
public class HiringManagerHandler
{
@InvocableMethod(Label='Create Related Position' Description='This Method will
Create a Related Position for the Hiring Manager')
Public static void CreateRelatedPositionRecord(List<Hiring_Manager__C>
hrRecords)
{
if(! hrRecords.isEmpty())
{
for(Hiring_Manager__C hr : hrRecords)
{
// Write the Code to Create a Related Position Record..
Position__C pos = new Position__C();
insert pos;
}
}
}
}
UseCase:
========
Configure a Process to Share the Record to the Selected User dynamically at
runtime based on the "Lookup Field".
Grant the Edit Access to the User, if the Account Record's Annual Revenue is
more than 50,00,000. Else grant the Read Only Access on the Record.
Business Logic:
---------------
public class AccountsShareHandler
{
@InvocableMethod(Label='Share Account Record' Description='This method will
Share the Account Record to Specified User.')
Public static void AfterInsert(List<Account> lstAccounts)
{
if(! lstAccounts.isEmpty())
{
List<AccountShare> shareAccountRecords = new List<AccountShare>();
accShare.AccountId = acc.Id;
accShare.UserOrGroupId = acc.Share_Record_To_User__c;
accShare.RowCause = 'Manual';
accShare.OpportunityAccessLevel = 'READ';
accShare.CaseAccessLevel = 'READ';
if(! shareAccountRecords.isEmpty())
{
Insert shareAccountRecords;
}
}
}
}