Batch @future calls?

Im trying to build what seemed like a simple SMS messaging application using Twilio’s api and running into a few strange momments.

So I have an SMS object that stores the messages and i wrote a simple trigger on there to actually call the code that sends the message.

Which works for one offs, but if i try to create a bunch of sms messages all at once i get an error saying i cant do more then 10 @future calls at once, and sadly because of the way twilio works each sms message has to be its own callout.

So i assume the answer is to batch the @future calls? but is there a way to have a trigger start a batch job??

Answer

Yes, you can start a batch from a trigger, but I would advise against it. From the documentation:

Use extreme care if you are planning to invoke a batch job from a
trigger. You must be able to guarantee that the trigger will not add
more batch jobs than the five that are allowed. In particular,
consider API bulk updates, import wizards, mass record changes through
the user interface, and all cases where more than one record can be
updated at a time.

A better solution would be to have a status on the SMS object and then have a scheduled job that then executes a batch for you.

Attribution
Source : Link , Question Author : Keith Mancuso , Answer Author : Daniel Hoechst

Leave a Comment