Since we can't update more than 10,000 records in trigger in real time I was thinking of automating the update of the remaining records through a batch called from a trigger.

So, Let's say for instance if we have 15,000 records and the number of records in trigger will exceed the governor limit 0f 10,000 when we try to do more than 10,000 dml in real time, so I want to run a batch from trigger itself when the number exceeds.Does it make sense?Is there a better way of doing it?

The problem I am facing is that we'll have to ensure that if there is no other instance of batch running before running it from trigger, but that batch is also being utilised for some other operation, so there is no way of identifying from AsyncApexJob object if the instance already exist for this particular operation.

Any help is appreciated! Thanks!

  • You cannot successfully submit 15000 records in a DML statement, so it isn't like your trigger would be called with the first 10000 and then have some magic access to the other 5000 records. As such I don't understand how you think you would invoke a batch from the trigger to handle this. Now, if you have processing that has the side-effect of updating 15000 records based on some input record set then you certainly can perform that side-effect processing in a batch that iterates the input record set.
    – Phil W
    Apr 13, 2021 at 9:47
  • In terms of ensuring that you have just one instance of a batchable running, take a look at this earlier answer.
    – Phil W
    Apr 13, 2021 at 9:49
  • The AsyncApexJob gives you access to the async class's name. To use the same batchable in multiple places but to only limit specific usages, provide a trivial extended version of the batchable so it has a distinct class name, only used in the limited processing context. The other usages would use the base class, and thus these usages would not be checked for in the AsyncApexJob table.
    – Phil W
    Apr 13, 2021 at 9:52
  • @PhilW I understand, so let's say the number of records are less than 10,000 which is possible with DML statement so if I want to process some thousands of records in real time and others through async (batch) from the trigger itself, I wanted to check if that's a feasible use case, I'll edit the example in the question as well. Thanks!
    – Isha
    Apr 13, 2021 at 10:00
  • 2
    Starting it from the trigger is the wrong place. You need to deal with this in the application code layer.
    – Phil W
    Apr 13, 2021 at 10:12


You must log in to answer this question.

Browse other questions tagged .