Problem Statement
I am trying to pass an instance of an Apex
type from LWC into an AuraEnabled
method. However, all of the attributes are coming through as null
.
MVR
Back End
public with sharing class MyController
{
@AuraEnabled(cacheable=true)
public static void doStuff(Model instance)
{
system.debug(JSON.serialize(instance));
// { "sampleAttribute": null }
}
public class Model
{
@AuraEnabled
public String sampleAttribute;
}
}
Front End
import doStuff from "@salesforce/apex/MyController.doStuff"
export default class MyComponent extends LightningElement {
...
const instance = {
sampleAttribute: "I am populated"
};
console.debug(instance);
// { sampleAttribute: "I am populated" }
doStuff({ instance })
.then(result => { ... })
.catch(error => { ... });
}
Question
Why are my attributes all cleared out by the time they arrive at the back end? They are clearly populated right before I send them. I even tried specifying each attribute via string key just to be sure that wasn't contributing to the problem, but it did not resolve the issue.
Workaround
For now I am using serialization to get the call to work.
Back End
@AuraEnabled(cacheable=true)
public static void doStuff(String payload)
{
doStuffWithModel((Model)JSON.deserialize(payload, Model.class));
}
public static void doStuffWithModel(Model instance)
{
// implementation
}
Front End
const instance = {
sampleAttribute: "I am populated"
};
doStuff({ instance })
.then(result => { ... })
.catch(error => { ... });
Expose Apex Methods to Lightning Web Components To expose an Apex method to a Lightning web component, the method must be static and either global or public. Annotate the method with @AuraEnabled.
...
These types are supported for input and output.
- Primitive—Boolean, Date, DateTime, Decimal, Double, Integer, Long, and String.
- sObject—standard and custom sObjects are both supported.
- Apex—an instance of an Apex class. (Most often a custom class.)
- Collection—a collection of any of the other types.
But the documentation clearly indicates use of Apex types is supported. Using JSON should not be necessary.