function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDCboxSFDCbox 

Invoking a Batch job from Trigger

Hello All,
I have got an After Update  trigger on Campaign which updates will update the Status of related Campaign Members (Lead/Contacts) when a campaign is updated to Active. I am invoking a batch job with a batch of 200, to update the lead and contact status. I know there can only be 5 batch jobs running simultaneously. How can i avoid getting the governer limits exception in case when i have a huge campaign?

Many Thanks in advance.
SFDCboxSFDCbox
To be precise , If i am invoking more than 5 batch does it add the 6th++ batch to a queue to process it later? Or Shall i check if there are already 5 batches running then schedule my batch to run after eg 30 minutes?
Vishal Negandhi 16Vishal Negandhi 16
Can't you modify this approach to be a better one - specially considering that you do have a batch involved. Schedule it to run (if that doesn't hurt the business)

What I mean is, whenever a Campaign is set as Active, capture the date in a new field which can be called as "Activated on". 

Now your scheduled batch can pick up all Campaign Members (Leads and Contacts) whose Campaign was activated today and update the records. 

This way, you avoid invoking batch from a trigger and update all the records too. 

Hope this helps. 
SFDCboxSFDCbox
Thanks for your thoughts Vishal but cannot use a scheduled Batch. Also i noticed that in above case, at a time we will only update one Campaign meaning only one Batch will be invoked from the trigger on campaign Object, at a time. However, now we want a similar batch on Campaign Member Object Trigger, so that when a campaign member is inserted/updated(in bulk) batch will process all the leads/contacts. Here i might have the issue of governer limits exceeding on no. of batches running ...Any ideas on this please