When we develop an AppExchange application, sometimes customer demand to support custom code execution in the product to execute their code before/after some business event. To provide such functionality we have to use Callable Interface along with dynamic execution of code. This post will explain dynamic code execution using Callable interface in Salesforce Apex.
Take an example, we are developing Health Care Provider application that will collect health care provider detail and it will validate their data as well like license and address etc. It is possible that customer (App user/consumer) want to perform some more validation before license or address is verified for that Health Care Provider.
To handle such a use case, we have to make our code extensible. This post will give step by step detail to make dynamic apex execution with Callable interface.
Steps to create Dynamic Code
We need to create two applications, one for a managed package that will support extensibility, and the other for managed package consumer applications that will extend functionality. These applications are created for this post POC only, actual applications can be of different use cases.
- Create Managed Application
- Add Extensible Code in Consumer Application
1. Create Managed Application
This step is for creating managed package/unmanaged package/Base code for our AppExchange product. As per our use case, we need to create Health Care Provider application. So we have to follow the below steps for creating a manage package code.
- Create Health Care Provider Object
- Create Dynamic Code Execution Setting
- Create Apex class to make extensible code
- Create Health Care Provider Object Trigger
a. Create Health Care Provider Object
Create Health Care Provider Object (HealthCareProvider__c) to hold information. Below fields can be created for this object
|Field Label||Field API||Data Type|
|License Verified||LicenseVerified__c||Check Box|
|Is Active||IsActive__c||Check Box|
b. Create Dynamic Code Execution Setting
Create a Custom Metadata type (DynamicCodeSetting__mdt) to support code extensibility. This metadata type will control extensible code execution. Below fields can be created in this metadata type
|Field Label||API Name||Data Type|
|Event Trigger Type||EventTriggerType__c||Picklist-|
|Execution Order||ExecutionOrder__c||Number(2, 0)|
Visibility of this custom metadata type will be set for all Apex code as it can be extended from any other namespace code.
|Visibility||All Apex code and APIs can use the type, and it’s visible in Setup.|
c. Create Apex class to make extensible code
Now we will write code to make application code extensible. We will use Type class to create instances of the dynamic classes which will be extended by the application consumers.
handleEvent method is creating an instance of a class that is passed from the consumer application. After instance creation, it will execute the provided method of that class. This is done using the Callable.call method in line#42.
updateHealthCareData method is validating data using third-party API calls. Application is giving functionality to add a code block to do some validation before a third party is called. Because third-party API calls can be expensive and we should only call when we have valid data.
d. Create Health Care Provider Object Trigger
Now create an object trigger to call the above handler class.
Now our first application is ready to use which will create a health care provider and validate entered detail. I have not added any third-party execution code, you can check that code from our other post Avoid Batch Apex and Use Queueable Class
2. Add Extensible Code
Adding extensible code is dependent on application (Managed Package) consumers. They can extend it if they have additional use cases which need to perform before the API calls otherwise we can leave it as it is as well.
We have to install the above manage package in our org. If you are just testing without the managed package then we can do both application codes in the same org.
executeDynamic method is adding code that will be executed from the managed application code. For this POST, I want that first and last names should not be blank. It can be different for you based on your business use case.
call method of Callable interface will call a specific method based on action. These actions are defined in metadata type.
Now we have to create one metadata record which will be used by managed package to control dynamic execution.
Test consumer application:
Now try to create health care provider record with and without first and last names and check validation should be fired when first and last name are empty.
Calling dynamic code is important in many scenerio especially to provide different behaviour for different type of business use case. Salesforce support dynamic apex execution using Callable interface. This way we can implement Open Closed Principle of SOLID Object Oriented Design for better code practice.