Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

For example, If I have a bunch triggers that aren't bulkified, so I'll end up hitting the governing limits were to do 100 insert opportunities in an separate apex class.

For batch apex, if I were to do an insert on opportunities, I assume it will fire those triggers and if the same governing limit apply?

Thanks.

share|improve this question

2 Answers 2

up vote 9 down vote accepted

Yes, triggers will fire when the Batch Apex runs and if you have large numbers of records in your batch you will hit the governor limit.

One solution would be to set the batch size to 1 so that every time the triggers fire there will only be 1 record. The batch class can be started with a batch size of one using something like this:

Id batchInstanceId = Database.executeBatch(new MyBatchClass(), 1); 

Another solution, and probably the best one, is to bulkify your triggers.

share|improve this answer

A third solution is to consider if you don't need the trigger processing to occur, in addition to BarCotter's good suggestions, is to set a static boolean variable that your triggers could use to avoid trigger processing.

public static boolean isRunningBatch;

Set to true at the start of your execute method and then in your triggers:

if (!isRunningBatch) { //run trigger code }

This only makes sense if you don't need the trigger logic during the batch however.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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