When a lead is created send Salesforce Lead and Account Data to SAP Hybris from rest API callout

70 Views Asked by At

When a lead is created in salesforce I want to send all lead details and its account details to SAP hybris from rest API callout.

Can anyone suggest any source code related to how to send salesforce data to an external systems like SAP?

Below is the source code I am using to create my custom API.

I want to use trigger also when lead is created or updated is any bulkification is needed or wrapper class needed for sending data from salesforce to external sites from rest API.

Code:

public class CalloutExampleToCreateMultipleContacts {

    public static final String CONSUMER_Key = ''; 
// paste your client id here
    public static final String CONSUMER_SECRET = '';
// paste the client secret here
    public static final String USERNAME = '[email protected]'; 
// paste your your name here
    public static final String PASSWORD = '';//paste the salesforce password and the security token
    

         public static void insertLead(List<Lead> LeadList){
        AccessTokenWrapper accessTokenWrapperObj = genrateToken();
        //this is the wrapper class of token generation 
        system.debug('--access token->'+accessTokenWrapperObj);
        if(accessTokenWrapperObj != null && accessTokenWrapperObj.access_token != null){

            String endpoint = 'https://cloudanalogy123-dev-             ed.my.salesforce.com/services/apexrest/createContact22/';
            String requestBody = JSON.serialize(LeadList);
            HTTP http = new HTTP();
            HttpRequest request = new HttpRequest();
            request.setBody(requestBody);
            request.setMethod('POST');
            request.setHeader('Authorization', 'Bearer '+accessTokenWrapperObj.access_token);
            request.setHeader('Content-type','application/json');
            request.setHeader('Accept','application/json');
            request.setEndpoint(endpoint);
            HttpResponse response = http.send(request);
            System.debug('Status code:'+response.getStatusCode()+'==>'+response.getBody());
            
            
         }
        }
    
         // function to get the access token

          public static AccessTokenWrapper genrateToken()
        {
         String requestBody =        'grant_type=password&client_id='+CONSUMER_Key+'&client_secret='+CONSUMER_SECRET+'&username='+USERNAME+'&password='+PASSWORD;
        String endpoint = 'https://login.salesforce.com/services/oauth2/token';
        HTTP http = new HTTP();
        HttpRequest request = new HttpRequest();
        request.setBody(requestBody);
        request.setMethod('POST');
        request.setEndpoint(endpoint);
        
        HttpResponse response = http.send(request);
        System.debug('response==>'+response.getBody()+'Status code:'+response.getStatusCode());
        if(response.getStatusCode() == 200){
            system.debug('');

        return 
             (AccessTokenWrapper)System.JSON.deserialize(response.getBody(),                    AccessTokenWrapper.class);
        }
         else{
            return null;
        }
        
        }
    
       // wrapper class to store the access token values
         public class AccessTokenWrapper{
        public string access_token;
        public string instance_url;
        public string id;
        public string token_type;
        public string issued_at;
        public string signature;
        
        
    } 
}  

  
1

There are 1 best solutions below

0
Emmett On

Taking the approach of writing a REST API call out has a few downsides. Especially if you have millions of rows of data. You need a solution that's scalable. Like:

  1. What happens when you have to reload all the data?
  2. What happens when the batchjob to load the data timesout and requires restarting?
  3. Is there a query-able record of what data was uploaded?
  4. Do you have a solution that has a service level agreement for how often data is refreshed?

What I recommend you do is set up a Heroku Connect Postgres mirror image of your Salesforce Instance. Incidentally, if you have an Enterprise License Salesforce gives you some "free" Heroku Connect resources.

After the Heroku Connect install, install a RDB synchronizing software-package such as SymmetricDS unto your Postgres instance and then have triggers of SymDS load into a relational database that SAP Hybris is attached to.