I was thinking of calling a batch class from my trigger. Is is advisable to do that? As batch will have asynchronous execution with respect to trigger.
Attribution to: Kaushik Ray
Possible Suggestion/Solution #1
You can call a batch from a trigger, but you need to be aware of potential limits you could hit. You can only have 5 batch jobs queued or executing at once. If your trigger calls batch jobs each time, then you could quickly exceed that limit. See http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm
Attribution to: Daniel Hoechst
Possible Suggestion/Solution #2
As Daniel has said, you can only have five batch job running so calling batch from a trigger is almost certainly not a good idea.
If you simply want to have asynchronous execution with respect to the trigger, then you can instead call methods annotated with the @Future annotation
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_future.htm
Attribution to: Doug B
Possible Suggestion/Solution #3
These answers are somewhat valid, but since you can query the state of a running batch you can ensure that your batch is a singleton.
Boolean isExecuting = (([SELECT COUNT()
FROM AsyncApexJob
WHERE ApexClassId IN (
SELECT Id
FROM ApexClass
WHERE Name = 'Blah'
)
]) == 0) ? false : true ;
if(isExecuting){
return;
}
Database.executeBatch(new Blah(),1);
if you protect your batch call you can also schedule the batch to run every hour. In that way it acts in almost realtime most of the time and a tail call executes every hour for anything that is missed.
Attribution to: ebt
Possible Suggestion/Solution #4
At a time only 5 apex batch jobs can be processed. Flex Queue does allow more than 5 concurrent batch jobs but it simply lines up the remaining jobs and keep their status as "Holding". So, apparently user feels that more than 5 jobs have been submitted but they all go in the "Holding" status.
Flex queue itself has a limit of holding up to 100 apex batch jobs only. Therefore, in a live org with thousands of users working simultaneously, lining up apex batch jobs from a trigger will create issues. If the apex flex queue reaches 100 jobs and at the same time a user tries to update something that is further triggering an apex batch then s(he) will see the following error on screen:
"System.AsyncException: You've exceeded the limit of 100 jobs in the flex queue for org X. Wait for some of your batch jobs to finish before adding more. To monitor and reorder jobs, use the Apex Flex Queue page in Setup".
Attribution to: Priyamvada Sharma
Possible Suggestion/Solution #5
In recent times, I have used the below approach and it works smoothly. Call the below code from trigger and put checks on the maximum number of jobs submitted in the flex queue + maximum number of jobs that can be scheduled. With this approach, we can process almost any number of recrods.
5 batches of 200 batch size will be concurrently processed. 100 jobs of 200 batch size can be in holding status in the flex queue. Once there are no vacant spaces in the flex queue, schedule the apex jobs for 10 mins. There could maximum 10 batch jobs of 200 batch size(on trigger context) can be scheduled and since the schedule time is 10 mins, we can expect that the flex queues are emptied by that time. So in the next iteration again records can be piled up into the flex queue.
// Checking the number of batch jobs have been submitted in the Apex Flex Queue. 100 is the limit.
Boolean isExecuting = (([SELECT COUNT() FROM AsyncApexJob WHERE ApexClassId IN (SELECT Id FROM ApexClass WHERE Name = 'BatchUpdatesAccountRelatedRecords') AND Status IN ('Holding')]) == 100) ? true : false ;
if(isExecuting){
//checking if the maximum number jobs have been scheduled or not. 10 is the limit.
if(!UtilClass.MaxScheduledJobsReached()){
// schedule the batch if maximum number of batch jobs is not scheduled.
String cronID = System.scheduleBatch(new BatchUpdatesAccountRelatedRecords(accnts), 'Customer Group Update', 10);
}else{
return;
}
}
Id bacthJobId = Database.executeBatch(new BatchUpdatesAccountRelatedRecords(accnts));
System.debug('Submitted Job Id'+bacthJobId);
Attribution to: Somnath Paul
Possible Suggestion/Solution #6
FlexQueue now allows more than 5 concurrent batch jobs at once. https://developer.salesforce.com/blogs/engineering/2014/05/flex-batch-apex-muscles-flexqueue.html
Attribution to: Neena
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31000