I'm a new in integration and I try to make a simple integration, but i'm stuck.
Scenario is simple: When record updates or creates, need to pass record data to external system. So I create a class and a trigger to run that class:
trigger AccTrigger on Account (after insert, after update) {
for(Account acc: Trigger.new){
if(Trigger.IsInsert && Trigger.IsAfter){;
integration.POST(acc.id);
}
if(Trigger.IsUpdate && Trigger.IsAfter){;
integration.POST(acc.id);
}
}
}
Class:
global with sharing class integration {
//@InvocableMethod
@future(callout=true)
global static void POST(Id AccountId) {
String secret = 'MySecret';
Blob requestBlob = Blob.valueOf(secret);
Blob hash = Crypto.generateDigest('MD5', requestBlob);
//Need to convert into hex to generate the equivalent of md5(string) method of PHP.
String requestSignature = EncodingUtil.convertToHex(hash);
//Generate json body
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('salesforceId', AccountId);
gen.writeEndObject();
String jsonS = gen.getAsString();
System.debug('jsonMaterials='+jsonS);
//Send call out to adminka
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('MyEndPoint');
request.setHeader('X-API-KEY', requestSignature);
request.setMethod('POST');
request.setbody(jsonS);
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 201) {
system.debug('Hooray!');
} else {
system.debug('Error='+response.getStatusCode());
}
}
}
All works great, but think that the use of future method is not fits my requirements. In a 24 hours it may be of thousands of records that have been updated.
Can anybody tells how to call post method on update without hitting any limits for a lot of dml?