Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

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.

2. The method should be pre-fixed with an annotation "@InvocableMethod()".

3. Invocable Method doesn't return any value to the calling environment.


Hence the Method return type should be always "void".

4. We can supply one or more parameters to the Method at runtime.

5. We can Supply the Primitive Values, SObject Type Values, Collection Type
of values to the InvocableMethod.

6. A Class can have only one "@InvocableMethod()" annotated method.

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.

Object Name: Hiring Manager Object


Start the Process: When the Record has been Changes
Criteria: When a Record has been Created.
Conditions:
Hiring_Manager__C:Location == 'Delhi'
Action Type: Trigger Apex
Method Name: Label Name of the Method
Parameters: Supply the Hiring Manager Record

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();

pos.Name = 'Java Developer';


pos.Location__c = hr.Location__c;
pos.HR_Email_ID__c = hr.Email_ID__c;
pos.HR_Contact_Number__c = hr.Contact_Number__c;
pos.Position_Status__c = 'New Position';
pos.Number_of_Positions__c = 1;
pos.Open_Date__c = system.today();
pos.Milestone_Date__c = System.today().AddDays(45);
pos.Minimum_Budget__c = 1400000;
pos.Maximum_Budget__c = 1900000;
pos.Travel_Required__c = true;
pos.Passport_Required__c = true;
pos.Position_Description__c = 'Required 4+ years of
Experience in Java Development.';
pos.Skills_Required__c = 'Required 4+ years of experience
in Core Java, Advanced Java, Oracle, Agile Methodology.';

// Make the Position to be Related to Hiring Manager..


pos.HiringManager__c = hr.Id;

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.

Object Name: Account Object


Start the Process: When the Record has been Changes
Criteria: When a Record has been Created.
Conditions:
Account: Share_Record_To_User__c != Null
Account: Active == 'Yes'
Action Type: Apex
Method Name: Label Name of the Method
Parameters: Supply the Account 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>();

for(Account acc : lstAccounts)


{
if(acc.Share_Record_To_User__c != null)
{
// Prepare the Share Details..
AccountShare accShare = new AccountShare();

accShare.AccountId = acc.Id;
accShare.UserOrGroupId = acc.Share_Record_To_User__c;
accShare.RowCause = 'Manual';
accShare.OpportunityAccessLevel = 'READ';
accShare.CaseAccessLevel = 'READ';

if(acc.AnnualRevenue >= 5000000)


accShare.AccountAccessLevel = 'EDIT';
else
accShare.AccountAccessLevel = 'READ';

// Add the Record to Collection for Bulkification..


shareAccountRecords.Add(accShare);
}
}

if(! shareAccountRecords.isEmpty())
{
Insert shareAccountRecords;
}
}
}
}

You might also like