I have a future method
that is called in trigger to create some records, and after that i use the records created in the future method
to associate them with other entity.
Since future methods
are asynchronous, when the code comes to the part of association, the record of future method
are not created yet, and i got an error.
I can't move the association code to the future method because it's used in other classes, and i can't remove the @future
annotation because it's used to avoid Mixed DML Operation error
.
How can i solve this problem to assure that the association is executed when the future method has finished?
This is sample code of what i'm trying to do:
public class MyTriggerHandler {
// Some logic
...
// Call of future Method
MyUtilClass.callFutureMethod();
// some logic
...
// Query records created in Future Method
List<MyObject__c> myList = new List<MyObject__c>([select Id,... from MyObject__c where ...]); // I have the error in this line
// Asociate object in my list with Other entity
...
}