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 = 'deepanshu.gupta@cloudanalogy.com';
// 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;
}
}