2

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?

1

You're allowed 250k future calls per day (combined with other things such as QUeueable, Batchable, and Schedulable calls); you should probably be okay. However, one thing you should do is batch the calls into groups of 100 for performance reasons.

List<List<Id>> batches = new List<List<Id>>();
List<Id> temp;
batches.add(temp = new List<Id>());
for(Account record: Trigger.new) {
  temp.add(record.Id);
  if(temp.size() == 100) {
    batches.add(temp = new List<Id>());
  }
}
if(batches[batches.size()-1].isEmpty()) {
  batches.remove(batches.size()-1);
}
for(List<id> batch: batches) {
  integration.post(batch);
}

Adjust your callout code to accept a list of ID values and iterate over all the values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.