function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shravan Kumar 71Shravan Kumar 71 

REST API Integration

I would like to pull user web activity details like website page visit link from an external API and display the same of the lead object page in Salesforce. I am not able to find a solution how to integrate the REST Apex code to Visfualforce to display the data. Kindly help me with the solution for this.
Best Answer chosen by Shravan Kumar 71
Manish Chandra 5Manish Chandra 5
Hi Shravan,

There could be two ways for achieving this. I have elaborated these on abit high level. Please let me know if you want more details:

1. Consuming web service hit from external API
     a. Create a connected App and generate Client id , client secret. Get the authentication done using refresh token flow, Web server authentication flow of OAuth 2.0. Follow below link for more details on authentication with salesforce.
     https://help.salesforce.com/articleView?id=remoteaccess_authenticate_overview.htm
    b. Create REST resource with 'POST' method to parse, process and save user activities in custom fields of Lead object. (Assuming you have Leads existing in system and they have user accounts in external API.)
   c. You can use either XML or JSON for data communication and use wrapper classes for parsing it. 
   d. You don't need to create VF page to show data, you can create fields as per your requirement and save the values from third party API into those fields.
 e. External API should hit your Salesforce API as soon as any activity happens at external API end.
 f. You can also use salesforce standard REST API but it won't be much flexible for this I need more details from you like your data volume, you need to save the data only to Leads or Users also?

2. Pulling data from External API using Batch Class
 a. Get authenticated with third party API using oauth 2.0. 
 b. Create a batch class and schedule this batch every 2 hours or 1 hour based on your data volume.
 b. make an HTTP callout to third party API endpoint after implementing interface in batch class i.e Database.AllowsCallouts
 c. get the reponse and after parsing it save it to lead object.

Thanks
  
      

All Answers

Manish Chandra 5Manish Chandra 5
Hi Shravan,

There could be two ways for achieving this. I have elaborated these on abit high level. Please let me know if you want more details:

1. Consuming web service hit from external API
     a. Create a connected App and generate Client id , client secret. Get the authentication done using refresh token flow, Web server authentication flow of OAuth 2.0. Follow below link for more details on authentication with salesforce.
     https://help.salesforce.com/articleView?id=remoteaccess_authenticate_overview.htm
    b. Create REST resource with 'POST' method to parse, process and save user activities in custom fields of Lead object. (Assuming you have Leads existing in system and they have user accounts in external API.)
   c. You can use either XML or JSON for data communication and use wrapper classes for parsing it. 
   d. You don't need to create VF page to show data, you can create fields as per your requirement and save the values from third party API into those fields.
 e. External API should hit your Salesforce API as soon as any activity happens at external API end.
 f. You can also use salesforce standard REST API but it won't be much flexible for this I need more details from you like your data volume, you need to save the data only to Leads or Users also?

2. Pulling data from External API using Batch Class
 a. Get authenticated with third party API using oauth 2.0. 
 b. Create a batch class and schedule this batch every 2 hours or 1 hour based on your data volume.
 b. make an HTTP callout to third party API endpoint after implementing interface in batch class i.e Database.AllowsCallouts
 c. get the reponse and after parsing it save it to lead object.

Thanks
  
      
This was selected as the best answer
Shravan Kumar 71Shravan Kumar 71
Hello Manish, Thank you for your quick response and providing the required information. I will follow the steps provided and reach out to you in case if I get stuck somewhere.
Shravan Kumar 71Shravan Kumar 71
Hi Manish,

Here is the code that I have come up with, however getting error message. Would you mind helping me to fix the issue :

global class LearnerActivityDetails {
    
    public list <String> webActivity;
    public list <String> emailSubject; 
    public list <String> emailOpened; 
    public list <String> emailClicked; 
    
    public PageReference getWebActivity () {
        Lead lead = (Lead)stdController.getRecord();
        lead = [SELECT Id, Email FROM Lead WHERE Id =:lead.Id];
        
        Endpoints__c webActivity = Endpoints__c.getOrgDefaults();
        String requestEndpoint = webActivity.Web_Activity_API__C + '/lead.email/';
        
        HttpResponse res = RestUtilities.httpRequestForWebActivity ('GET', requestEndpoint, token, jsonBody, null);
        
        if (res.getStatusCode() == 200) {
            
            
            Map<String, String> results = (Map<String, String>)JSON.deserializeUntyped(res.getBody(), LearnerActivityDetails.class);
            List<Map<String,String>> listResults = new List<Map<String,String>>();
            webActivity = List<String>results.get('webActivity');           
            for (String webActivityResult :  webActivity) {
                webActivityResult.add((Map<String, String>)webActivityResult);
            }
            
            emailSubject = List<String>results.get('emailSubject');           
            for (String emailSubjectResult :  emailSubject) {
                emailSubjectResult.add((Map<String, String>)emailSubjectResult);
            }
            
            emailOpened = List<String>results.get('emailOpened');
            for (String emailOpenedResult :  emailOpened) {
                emailOpenedResult.add((Map<String, String>)emailOpenedResult);
            }
            
            emailClicked = List<String>results.get('emailClicked');
            for (String emailClickedResult :  emailOpened) {
                emailClickedResult.add((Map<String, String>)emailClickedResult);
            }
            
        } 
        
    }
    
    public class webActivity {
        String Test1 = 'www.simplilearn.com' ;
        
    }
    
    public class emailSubject {
        String Test2 = 'Hello!';
    }
    
    public class emailOpened {
        String Test3 = 'No';
    }
    
    public class emailClicked {
        String Test3 = 'Yes';
    }
}
Shravan Kumar 71Shravan Kumar 71
Hi Manish,

Ignore the above message. I managed to modfiy the code and was able to run it successfully. Thanks a lot for your eariler inputs.

Thanks