6

Here's what I'm working with:

// Client-side controller

({
    handlePress : function(component, event, helper) {
        var action = component.get("c.returnClassic");
        action.setCallback(this,function(response){
                if (response.getState() === "SUCCESS"){
                    console.log(response.getReturnValue());
                }
            });
        $A.enqueueAction(action);
    }
})

// Server-side controller

public class LightningController {

    public class Classic{
  //      @AuraEnabled
        public String s {get; set;}
        @AuraEnabled public Account a {get; set;}
        @AuraEnabled public Integer i {get; set;}

        Classic(String s, Integer i){this.s=s; this.i=i; this.a=new Account(Name='hello');}
    }

    @AuraEnabled
    public static Classic returnClassic(){
        return new Classic('one',2);
    }
}

Whenever I make the String in my class AuraEnabled, I get undefined logged in the console. Whenever the string is not AuraEnabled, the console logs the object with the integer and the account object with the name string.

It looks like someone else was able to get something similar to work: @AuraEnabled Support for Apex Class Return Types? Although I can't see what I'm doing differently that would prevent my code from working.

UPDATE

It seems that the issue isn't with returning a string. The issue was caused by using the letter lower-case s as the property name. Lower-case r has a similar issue, and I created a new question more specific to the issue here: Returning an object from an AuraEnabled method with properties named r or s

UPDATE 2

Passing data up to the server is difficult as well and stringifying it before sending it can help avoid some of those issues. Other people also ran into these problems and wrote about it here: sObject array parameter in Lightning causes Internal Salesforce.com Error in Apex controller

1 Answer 1

3

You can try doing what Peter Knolle mentioned in the post you referenced, to just return a JSON string that contains the data in your class, instead of returning 'Classic'. Basically you would just have to serialize your class instance

@AuraEnabled
public static String returnClassic(){
    return JSON.serialize(new Classic('one',2));
}

And then in the controller just parse the string into a Javascript object.

({
    handlePress : function(component, event, helper) {
        var action = component.get("c.returnClassic");
        action.setCallback(this,function(response){
                if (response.getState() === "SUCCESS"){
                    console.log(JSON.parse(response.getReturnValue()));
                }
            });
        $A.enqueueAction(action);
    }
})
2
  • 1
    it will be good to return Object instead of doing JSON serializing.
    – Ankuli
    Commented Jul 23, 2015 at 13:48
  • I feel that we should be able to directly send and receive objects and arrarys, but until they improve this aspect of the framework, the @Peter Knolle method seems to be the best where method parameters and return values are serialized/sent/parsed and stringified/sent/deserializeUntyped.
    – martin
    Commented Jul 25, 2015 at 3:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .