Can we pass wrapper to future method? Even if wrapper class having only primitive datatype arguments.
-
3As an FYI, an Apex class is a class, not a wrapper. Wrappers are classes intended to encapsulate some other object (e.g. an SObject) while providing some value-added capabilities not available with the "wrapped" object. This is a special case usage of an Apex class.– Phil WCommented Dec 1, 2019 at 10:52
3 Answers
I would also recommend using Queueable, since it's more modern approach and provides more flexibility.
You can pass wrapper, but for that, you'll need to serialize/deserialize that parameter.
// method
public void method(String wrapper) {
WrapperClass value = (WrapperClass) JSON.deserialize(wrapper, WrapperClass.class);
// implementation
}
// call
method(JSON.serialize(wrapper));
It doesn't look like future methods can accept a wrapper class. @future
methods can only take primitive types according to the official documentation:
The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types. Methods with the future annotation cannot take sObjects or objects as arguments.
If you need something that runs asynchronously but can take any kind of data type, consider using Queueable
apex.
The above techniques will work however, you could also convert the Wrapper or any complex type to a String which is a primitive.
Once converted into a String you can them pass that string as a parameter to the future method in consideration.
Use the Json.serialize(complexType)
in Apex to convert to an equivalent Json string value.