How to create your own Interface and use it efficiently in Apex Salesforce

Go to 'Apex Classes', click 'New' and create a new interface like below:
public interface IntegrationServiceInterface{
      String doHttpCallout(String inputParam);
}
This is your base interface, you can declare number of methods here based on your requirements, I have just created one. Create a generic abstract class which will implement this interface and its methods virtually like below:
public abstract class IntegrationServiceImpl implements IntegrationServiceInterface{
     public virtual String doHttpCallout(String inputParam){
          return 'This is IntegrationServiceImpl';
     }
 }
Create your own actual class which will extend this IntegrationServiceImpl abstract class and will override the methods:
public class MyIntegrationServiceImpl extends IntegrationServiceImpl{
     public override String doHttpCallout(String inputParam){
         return inputParam + ' :: This is MyIntegrationServiceImpl';
     }
 }
This class depends on the integration type, so you have to create different classes for each type of integration you have in system.

Now create a custom setting of type 'List', which will store the name of your integration class which you will create for each integration. Here I have created custom setting called 'Integration Information' with api name 'IntegrationInformation__c' and a field 'Class name' with api name 'Class_name__c'. Now create a new record in this custom setting with name ='MyIntegration' and class name = 'MyIntegrationServiceImpl'.

You have to create a new record each time when you create a new integration class like 'MyIntegrationServiceImpl'. Now you need to create one more generic class which will actually call this integration using this custom setting and return the output of callout.

public class IntegrationFactory{
   public static String callIntegrationService(String inputParams, String integrationName){
     String implClassName = IntegrationFactory.fetchIntegrationClassName(integrationName);
     Type typeOfClassName = Type.forName(implClassName);
     IntegrationServiceInterface oServiceImpl = (IntegrationServiceInterface)typeOfClassName.newInstance(); 
         
     return oServiceImpl.doHttpCallout(inputParams);
   }
     
   public static String fetchIntegrationClassName(String integrationName){
     String className = '';
     IntegrationInformation__c integrationInfo = IntegrationInformation__c.getInstance(integrationName);
     if(integrationInfo != null && String.isNotBlank(integrationInfo.Class_name__c)){
         className = integrationInfo.Class_name__c;
     }
         
     return className;
   }}

You can call this class and its method 'callIntegrationService' from anywhere like VisualForce page, Lightning Component, here you just need to pass the name of the integration and string input parameter in json format which will include all parameters like callout method, access token, endpoint etc., and it will call the integration for you and return the output.
 

  String output = IntegrationFactory.callIntegrationService('Sample input', 'MyIntegration');
  system.debug('Output  :-> '+output);
 
  Output  :-> Sample input :: This is MyIntegrationServiceImpl
Now if you want to integrate another integration, you just have to follow below steps: Create your new integration class like MyIntegrationServiceImpl , I have created HisIntegrationServiceImpl
public class HisIntegrationServiceImpl extends IntegrationServiceImpl{
     public override String doHttpCallout(String inputParam){
         return inputParam + ' :: This is HisIntegrationServiceImpl';
     }
 }
And create a new entry in 'Integration Information' custom setting, name as 'His Integration' and class name as 'HisIntegrationServiceImpl', call this now with the same InegrationFactory class.
That’s ALL, you need to do for new integration.

  String output = IntegrationFactory.callIntegrationService('Sample input 2', 'HisIntegration');
  system.debug('Output  :-> '+output);
 
  Output  :-> Sample input 2 :: This is HisIntegrationServiceImpl
Newer Oldest

Related Posts

Post a Comment

Subscribe Our Newsletter