Do not use JSON.stringify to pass params to Apex
Serializing parameters before passing them to Apex is considered bad practice because it leads to unclean code and negatively impacts performance.
Do NOT use JSON.stringify to pass params to Apex
Serializing parameters before passing them to Apex is considered bad practice because it leads to unclean code and negatively impacts performance.
@AuraEnabled
public static void call(String serializedParam) {
MyWrapper wrapper = (MyWrapper) JSON.deserialize(serializedParam, MyWrapper.class);
// ...
}const params = {
// ...
};
await call({
serializedParam: JSON.stringify(params)
});Apex data structures have corresponding equivalents in LWC that can be used directly. Please take a look at the table below for details.
APEX
LWC
Primitive Types e.g Boolean, String, Integer
Primitive Types
List<Type> e.g List<String>
Array [ ]
Custom Objects e.g Account, Custom__c
Object { }
Class e.g WrapperClass
Object { }
Map<Type, Type> e.g Map<String, String>
Object { }
Primitive Types
@AuraEnabled
public static void call(String stringParam, Integer integerParam) {
}await call({
stringParam: 'Some String',
integerParam: 42
});List<Type>
@AuraEnabled
public static void call(List<String> myList) {
// ...
}const stringArray = [
'Some String 1',
'Some String 2',
'Some String 3',
];
await call({
myList: stringArray
});Custom Objects
@AuraEnabled
public static void call(Account account) {
// ...
}const newAccount = {
Name: 'Account Name'
};
await call({
account: newAccount
});Wrapper Class
public class MyWrapper {
@AuraEnabled
public String myString {get; set;}
@AuraEnabled
public Integer myInteger {get; set;}
}@AuraEnabled
public static void call(MyWrapper myWrapper) {
// ...
}const myWrapper = {
myString: 'Some String',
myInteger: 43
};
await call({
myWrapper: myWrapper
});Map<Type, Type>
@AuraEnabled
public static void call(Map<String, String> myMap) {
// ...
}const myMap = {
param1: 'Some String 1',
param2: 'Some String 2'
};
await call({
myMap: myMap
});

