I have a non future method that I use for before insert in criteria A, how can I use this method to a future method without copying the method and adding @future because I also need this for a particular criteria(criteria B) in after insert trigger event. Thanks
-
Put it in a public class and make the method public – cropredy Aug 27 '17 at 3:05
Yes, you can call normal methods from a future method. Typically, when I need a method for multiple purposes, I'll write a future method that simply calls the non-future method.
@future public static void doSomethingAsync(Id recordId) {
doSomething(recordId);
}
public static void doSomething(Id recordId) {
// Do stuff
}
Note that because of the limitation of future methods, it's often necessary to requery the data that you need, so a more typical solution looks like this:
@future public static void doSomethingAsync(Id[] recordIds) {
SObject[] records = [SELECT ... FROM ... WHERE Id = :recordIds];
doSomething(records);
}
public static void doSomething(SObject[] records) {
// Do stuff here
}