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
Ad CadAd Cad 

Calling a future method from a Trigger which is invoked from a Batch

Hi,

I have a trigger that,amongst other things, adds a User to a Public Group. To achieve this, and not get a 'Mixed DML' error, the code to add the User to the Public Group is wrapped up in an @future method, so it executes in its own transaction.

Now, a new requirement has come along that requires a lot of records to be updated, and so this trigger - and the @future method - will get called thousands of times. I've wrapped all this up in a Batch class to achieve this.

But, calling an @future method from a Batch (effectively, calling an Async method from another Async method) is not permitted.

I tried, in the Trigger, detecting the process was being called from a batch and then using a not-@future method - but, of course, that fails again with a 'Mixed DML' error.

Any suggestions as to how I can get around this? Please don't get too focussed on this particular scenario, as I have a few similar ones, and what I'd really like is a general solution to this.

Thanks for your suggestions!

Best Answer chosen by Ad Cad
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

In this scenerio did you try achieving this using Queueble apex. So first perform the operation and enqueue the method to add the user to public group so you can achieve this with out any error.

Please find the help article (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm) for the same.

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

In this scenerio did you try achieving this using Queueble apex. So first perform the operation and enqueue the method to add the user to public group so you can achieve this with out any error.

Please find the help article (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm) for the same.

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Mary FieldsMary Fields

In Salesforce, calling a future method from a trigger is not recommended as it can lead to unpredictable behavior. This is because future methods run asynchronously and the order of execution is not guaranteed. If a future method is called from a trigger, the trigger may complete execution before the future method is finished, which can result in incomplete data updates.

Additionally, calling a future method from a batch apex can also cause issues as batch apex operates in its own context, separate from triggers and future methods. If a batch apex invokes a trigger that calls a future method, it is possible for the future method to run after the batch apex has completed and not receive updated information from the batch operation.

Instead of calling a future method directly from a trigger or batch apex, consider using Queueable apex or asynchronous apex methods. These methods provide a more reliable and predictable way to handle long-running operations in Salesforce.

 

Thanks,

https://www.wellcaremedicare.org/