1

I am calling an apex method from lwc which returns a wrapper class like below.

public class LoadDataWrapper{
    Account acc;
    List<Opportunity> opps;
    List<Product2> prods;
}

Is there a way to deserialize response to a similar class structure in lwc javascript file so I don't end up creating too many properties or variables? Even better if I can probably add such wrapper class structures in a separate module and import it in component js file.

Is my thinking right?

Also, How do I reference the response directly in the template file?

For eg: {Account.Name} etc.

Edit: I think Im looking for writing client side class to handle the response from apex. Right now, Iam creating a javascript object with structure similar to the apex response and initializing it to null values. This is helping with avoiding undefined type error when accessing the properties in template. Is there any other approach?

1
  • you can use Map<String, Object> to have it dynamic, instead of the wrapper class Sep 4, 2021 at 15:34

1 Answer 1

5

You need to AuraEnable the attributes and make them public in order to read them on the client side. You may also need to add getter/setter syntax, especially if you want to pass the same model back to the server.

public class MyWrapper
{
    @AuraEnabled
    public Account record { get; set; }
}
2
  • Of course, this generates "anonymous" JavaScript objects - there is no client-side class for them and the best you can do is provide a JSDoc type def to allow your IDE to better understand typing for these objects.
    – Phil W
    Sep 5, 2021 at 8:43
  • I think Im looking for writing client side class to handle the response from apex. Right now, Iam creating a javascript object with structure similar to the apex response and initializing it to null values. This is helping with avoiding undefined type error when accessing the properties in template. Is there any other approach?
    – Uday
    Sep 5, 2021 at 13:24

You must log in to answer this question.

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