1] Get Access token from external system.
public static String getAccessToken(){
1. Create a custom setting (Integration_Config__c) with following fields: Client Id, Client Secret, Endpoint Url, Token Url
2. Create records for storing above details one for different environments.
3. Get the details for particular environment.
Integration_Config__c apiConfig = Integration_Config__c.getValues(System.label.API_Token);
4. Create HTTP request object.
HttpRequest req=new HttpRequest();
5. Set the token endpoint url to get token.
req.setEndpoint(apiConfig.Token_Url__c);
req.setMethod('POST');
req.setBody('grant_type=client_credentials'+'&client_id='+EncodingUtil.urlEncode(apiConfig.Client_Id__c, 'UTF-8')+'&client_secret='+EncodingUtil.urlEncode(apiConfig.Client_Secret__c, 'UTF-8'));
Http http=new Http();
6. Make HTTP callout.
HttpResponse response=http.send(req);
if(response != null && response.getStatusCode()==200){
7. Based on response JSON of token api, create a wrapper token response class.
tokenResponse respWrap=(tokenResponse)JSON.deserialize(response.getBody(), tokenResponse.class);
return respWrap.access_token.trim();
}
return null;
}
2] Make REST Callout
Class: ApexHandlerForObject
Function: public static void sync(String Data)
Map<id,Object> issuesData = (Map<id,Object>)JSON.deserializeStrict(data, Map<id,Object>.class);
// For each object record make callouts following way:
Integration_Config__c apiConfig = Integration_Config__c.getValues(System.label.API_Token);
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(apiConfig.endpoint_Url__c+'/'+apiName);
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
try{
request.setHeader('Authorization','Bearer '+accesstoken);
request.setTimeout(120000);
// You can use certificate this way.
request.setClientCertificateName('Sample-Rest-Self-Signed');
// requestBody JSON as expected by target API. This should be string.
// If target API is bulkified than serialize list of objects, else serialize single object.
request.setBody(JSON.serialize(object record));
HttpResponse response = http.send(request);
system.debug('response:'+response.getBody());
system.debug('Status Code : '+ response.getStatusCode());
if( response.getStatusCode() == 200){
if(response.getBody() != null){
respWrap=(ResponseWrapper)JSON.deserialize(response.getBody(), ResponseWrapper.class);
}
}
}
catch(Exception e){
}
3] To use Future Method, define future method as follows
class: ApexHandlerForObject
@future(Callout=true)
public Static void syncFuture(String data){
// Invoke function making a callout.
ApexHandlerForObject.sync(data);
}
4] Make future callout from apex trigger handler
ApexHandlerForObject.syncFuture(JSON.serialize(object or map))
5] Can also use Queueable method to make callout
// This is how to call Queueable Method.
system.enqueueJob(new IntegrationQueue(JSON.serialize(object or map)));
// This is Queueable class implementation
public class IntegrationQueue implements Queueable,Database.AllowsCallouts {
String data;
public IntegrationQueue(String data){
this.data=data;
}
public void execute(Queueablecontext context){
if(data != null){
// Innvoke function making a callout.
ApexHandlerForObject.sync(data);
}
}
}