+ Start a Discussion
Charles Naccio 1Charles 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
       /*******************************************************************************************************
        * @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!
 
logontokartiklogontokartik
Yes, I think Salesforce is supposed to fix this, not sure if they did it yet. One workaround that might work is to seriliaze the object as JSON String and then pass it as String to Apex Controller and then deserialize it. Let me know if this works.

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.
Charles Naccio 1Charles Naccio 1
Yea that's what I ended up doing; seems to be working pretty well. Thanks for the confirmation though that this is an acceptable workaround for now!
Devendra Dhaka 9Devendra Dhaka 9
Salesforce has fixed this in Spring 16.
ssgmailssgmail
I am on Spring 16. I am getting "Internal Server Error" when I pass custom class as parameter. Simple string parameters work fine. 
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?
Bhuvan PashamBhuvan Pasham
This bug seems to be still there even in Summer 16 release. Wonder why SFDC team is NOT fixing such critical platform issues. Or they NOT monitoring this developer forum?
Francesco Carmagnola 10Francesco Carmagnola 10
The same issue here... Take a look at this question I've already opened:

https://developer.salesforce.com/forums/ForumsMain?id=9060G000000Xe5lQAC

Regards
smriti kumari 10smriti kumari 10
Hello,

Use JSON.stringify to convert your object to a String in your lightning component
action.setParams({ strFilters : JSON.stringify(component.get("v.lstFilters")) });
In your apex method, you can convert it back:
 
ClassName[] lstFilters = (List<ClassName>)System.JSON.deserializeStrict(strFilters, List<ClassName>.Class);

 
Petr ChvalaPetr Chvala
Sadly still broken in v 40.