3

Can we pass wrapper to future method? Even if wrapper class having only primitive datatype arguments.

1
  • 3
    As 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 W
    Commented Dec 1, 2019 at 10:52

3 Answers 3

8

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));

4

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.

2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .