2

I am having trouble passing an apex wrapper class from LWC into APEX.

Apex

@AuraEnabled
public static void myMethod(SomeData theData) {
    system.debug('theData is ' + theData);
}

public class SomeData {
    public String theString;
    public Object theObject;
}

LWC

connectedCallback() {
    const myData= {
        'theString': 'Test',
        'theObject': true
    };
    myMethod({theData: myData})
    .then(result => {
        if (result) {
            
        }                   
    } catch(err) {
        
    }
}

When I run the code above what I get from APEX is [theString=null, theObject=null].

However if I modify the signature of the APEX method to be public static void myMethod(String theString, Object theObject) { and respectively pass that from the JS side myMethod({theString: myString, theObject: myObject}) where myString = 'Test'; and myObject = true that gets parsed properly in APEX and I can see it in the debugs.

I also tried specifying the sobjectType when instantiating the JS object like but that didn't help either:

const myData= {
    'sobjectType': 'SomeData',
    'theString': 'Test',
    'theObject': true
};

1 Answer 1

3

You need to @AuraEnable your attributes.

Incorrect

public class SomeData {
    public String theString;
    public Object theObject;

Correct

public class SomeData {
    @AuraEnabled public String theString { get; set; }
    @AuraEnabled public Object theObject { get; set; }
}
6
  • Oh my god, the most obvious. Thank you. One more question - why can't I access the variables without specifying { get; set; } at the end? I remember VisualForce required those but I thought that for LWC all I need to do is say @AuraEnabled. At least that is the case if I pass them from APEX to LWC.
    – Arthlete
    Aug 26, 2021 at 22:32
  • +1 Might also want to point out case-sensitivity, just in case.
    – sfdcfox
    Aug 26, 2021 at 22:33
  • 1
    @Arthlete The { get; set; } was previously required because of a bug (discussed in this comment chain). I'm not sure if it got fixed. You can try it without implicit getter/setter, but be aware it may break.
    – sfdcfox
    Aug 26, 2021 at 22:36
  • @sfdcfox Yes without the { get; set; } it will not accept the values I pass from the front end. Thank you for the link!
    – Arthlete
    Aug 26, 2021 at 23:26
  • 1
    I think it's only passing from front end to back end where it's needed.
    – Adrian Larson
    Aug 26, 2021 at 23:29

You must log in to answer this question.

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