Use Custom Apex Types as Apex Method Parameters
Passing data from JavaScript that maps to a custom Apex type
parameter is convenient as you don’t need to manually convert data to an Apex custom class
with the associated type checking. This feature leads to simpler code and better developer
productivity.
Where: This change applies to orgs with Lightning components in Lightning Experience, Salesforce Classic, and all versions of the Salesforce app.
How: Each property in the Apex class must have an @AuraEnabled annotation, and a getter and setter. For example, if you have a MyCustomApexClass Apex class:
public class MyCustomApexClass { @AuraEnabled public String s {get; set;} @AuraEnabled public Integer i {get; set;} @AuraEnabled public List<string> l {get; set;} @AuraEnabled public Map <string, string> m {get; set;} }
Now, you can use MyCustomApexClass as the parameter type for a method in an Apex controller.
public class MyController { @AuraEnabled public static MyCustomApexClass customApexParam(MyCustomApexClass v) { System.debug(v); return v; } }
Call the customApexParam() Apex method from a JavaScript controller. The data structure matching the shape of MyCustomApexClass is set in the action.setParams() call.
var action = component.get("c.customApexParam"); var val = { s: 'my string', i:10, l: ['list value 1','list value 2'], m: {k1: 'map value'} }; action.setParams({ v : val }); action.setCallback(this, function(response) { console.log(response.getReturnValue()); }); $A.enqueueAction(action);