Wednesday, January 31, 2018

Salesforce: Calling batch methods from other batch methods

Have you just had the fun situation where a Salesforce batch has run, which caused something (such as a trigger, etc) to cause another batch to attempt to run?

If you have, then you know the frustration of getting the joyous error message below:


Database.executeBatch cannot be called from a batch or future method.

There are two ways of addressing the "Salesforce Feature". Rewrite your classes to not run if either System.isFuture() or System.isBatch() is true.



or...



Change the API version of all effected classes and triggers to exactly v27.

This single version of the api allows batches to be called from batches. This is obviously not a "good" solution, but if it's a stupid solution, and it works, it's still a solution.



0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.