0

I wanted to Bulkify my code to be able to update/create a long list of account for which I need to callout to my api.

I know that I can't do more than 50 callouts per future methods. But using Queueable Jobs I can't add up to 50 jobs to the queue.

So I'm sending the list of account inside a JSON with one CallOut :

Code In my trigger :

trigger AccountTrigger on Account (after insert, after update) {

List<Account> lstJson =new List<Account>();
for(Integer i=0;i<Trigger.new.size();i++){
    Account account = Trigger.new[i];
lstJson.add((account));
}

/// Then i send my list of account to my Future Task
Map<string, List<Account>> accountMap = new Map<string, List<Account>>();   
// Normally I should only Serialize the JSONLIst but our api has a problem...
accountMap.put('items', lstJson);
FutureTaskCreator.sendNotification(JSON.serialize(accountMap));

Then I call my API inside the callout :

public class FutureTaskCreator {

@future(callout=true)
public static void sendNotification(String json_string){
  //my code
}
}

Is it a good way to implement it ? I've hit a limititation in both Queue and Callout...

Thanks for feedback to improve the method

2
  • 1
    did you consider chained queueables? this avoids the limit issue
    – cropredy
    Dec 4, 2019 at 16:59
  • Yes, but I reached the same type of limit I can't Queue as much as I want. I had trouble "splitting" the list.
    – Benoît
    Dec 5, 2019 at 9:05

1 Answer 1

0

One way of doing it would be to split the list of accounts into 5 batches of 10 each and invoking 5 queueable calls which will do the callout and rest of the processing.

List<Account> lstJson =new List<Account>();

Split the final list into 5 lists and call queueables 5 times

FutureTaskCreator.sendNotification(JSON.serialize(accountMap1));
FutureTaskCreator.sendNotification(JSON.serialize(accountMap2));
FutureTaskCreator.sendNotification(JSON.serialize(accountMap3));
FutureTaskCreator.sendNotification(JSON.serialize(accountMap4));
FutureTaskCreator.sendNotification(JSON.serialize(accountMap5));
4
  • I get the idea, but I don't think it is elegant. So I would stick to my solution calling one time with a JSON containing the full list.
    – Benoît
    Dec 5, 2019 at 9:07
  • @Benoît, this will also be easy on other limits, again your call
    – RedDevil
    Dec 5, 2019 at 10:20
  • What would be the limit ?
    – Benoît
    Dec 6, 2019 at 14:01
  • In your solution you talk about queuables, what do you mean by that ? About the code I should create a for loop where I split my list into as much need to not reach 50 calls ?
    – Benoît
    Dec 6, 2019 at 14:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .