You need to sign in to do that
Don't have an account?
Charles Naccio 1
Unable to pass complex object parameters from Lightning client controller to Apex server controller
Objects with a single layer work fine, but when I try to use an object that goes deeper than one layer I receive an unknown server error when trying to access the variable on the server side user parameter.get('objectPropertyName') see code below.
So this code works as gateway is a simple string. However whenever I try to make gateway an object I start getting errors
This code gives me an error when trying to access the parameter map in the server side controller.
Here's my server side controller method:
So in summary whenever gateway is a simple string everything works... The second I try to use an object I start getting errors. I'd prefer to use one of my Apex classes instead of a Map<String, Object> for the parameter, but I was having no luck with that. Any help is much appreciated!
So this code works as gateway is a simple string. However whenever I try to make gateway an object I start getting errors
/******************************************************************************************************* * @description Test saving configuration */ var saveConfiguration = component.get("c.testSomething"); // Set save configuration parameters. saveConfiguration.setParams({ testSome : 'Hello World!', config : { gateway: 'Stripe' } }); // Setup save configuration callback saveConfiguration.setCallback(this, function(a) { if (a.getState() === "SUCCESS") { // Do something with return value } else if (a.getState() === "ERROR") { $A.error("Errors", a.getError()); $A.log("Errors", a.getError()); } }); // Queue the transaction action $A.enqueueAction(saveConfiguration);
This code gives me an error when trying to access the parameter map in the server side controller.
/******************************************************************************************************* * @description Test saving configuration */ var saveConfiguration = component.get("c.testSomething"); // Set save configuration parameters. saveConfiguration.setParams({ testSome : 'Hello World!', config : { gateway: 'Stripe' } }); // Setup save configuration callback saveConfiguration.setCallback(this, function(a) { if (a.getState() === "SUCCESS") { // Do something with return value } else if (a.getState() === "ERROR") { $A.error("Errors", a.getError()); $A.log("Errors", a.getError()); } }); // Queue the transaction action $A.enqueueAction(saveConfiguration);
Here's my server side controller method:
@AuraEnabled public static void testSomething(String testSome, Map<String, Object> config) { System.debug(config.get('gateway')); }
So in summary whenever gateway is a simple string everything works... The second I try to use an object I start getting errors. I'd prefer to use one of my Apex classes instead of a Map<String, Object> for the parameter, but I was having no luck with that. Any help is much appreciated!
The method that can be used in the JS is
$A.util.json.encode()
And in Apex you can use JSON.deserializeUntyped or JSON.deserialize
Hope this helps.
I am not sure if this is a different error than what Charles Naccio 1 encountered or the same error. Has anyone succeffuly passed custom classes as parameter to Apex auraenabled functions in lightning without serializing and deserielizing yourself? Is this a known SFDC lightning bug?
https://developer.salesforce.com/forums/ForumsMain?id=9060G000000Xe5lQAC
Regards
Use JSON.stringify to convert your object to a String in your lightning component In your apex method, you can convert it back: