2

I have an apex controller that I'm using to run a SOQL query for some data. Initially, I just had the method return the actual Object, but as I'm making reference to many different parts of data, I've created a new Custom Object (inside the controller) to serve as a "ViewModel" so I can initialize field values and navigate the object more directly in the consuming .js code.

APEX Controller Code:


public with sharing class FundsRequestCostsController {
    @AuraEnabled
    public static RequestTotals getCosts(Id id) {
        if (id == null) { throw new NullValueNotAllowedException(); }

        FundsRequest__c request =
                [SELECT
                    TotalCost__c,
                    ...
                    Campaign__r.NumberOfOpportunities,
                    ...
                FROM
                        FundsRequest__c
                WHERE Id = :id];
        RequestTotals totals = new RequestTotals();

        totals.TotalCost = (request.TotalCost__c == null) ? 0 : request.TotalCost__c;
        ...
        return totals;
    }

    public class RequestTotals {
        public Integer NumberOfOpportunities;
        public Decimal TotalCost;
        ...
    }

    public class NullValueNotAllowedException extends Exception {}
}

Now, when try to access the response object in the .js controller a.getReturnValue() returns an empty object.

Before I made the change to introduce the custom RequestTotals object, it worked fine (having the .getCosts() method return a FundRequest__c object)

I've checked a.getError() and it contains an empty Array.

1

2 Answers 2

3

There are two particular bugs

1.The nested or inner classes always fails for me .I always create a separate class and never mix as inner class if i am using the apex for lightning component

2.Make sure to mark all the properties of the class as "AuraEnabled"

 public class RequestTotals {
    @Auraenabled
    public Integer NumberOfOpportunities;
    @Auraenabled
    public Decimal TotalCost;
    ...
}
5
  • The class you need is RequestTotals and not NullValueNotAllowedException Commented Nov 9, 2016 at 16:04
  • I'm not able to make the changes you suggest. First, the developer console shows this error when I try to decorate these classes with @AuraEnabled : RequestTotals: Types cannot be marked as AuraEnabled Second, when I tried to create a new APEX Class for the NullValueNotAllowedException, I get this error: INVALID_FIELD_FOR_INSERT_UPDATE NullValueNotAllowedException: Exception class must extend another Exceptoin class Commented Nov 9, 2016 at 16:12
  • You do not need new class for exception throwing and you can create a new class named RequestTotals as I have shown and mark all the properties as @auraenabled . Commented Nov 9, 2016 at 16:19
  • Oh, whoops. I didn't pay close enough attention to see that the decoration was on the fields and not the class. Thank you! Commented Nov 9, 2016 at 16:41
  • If that answers be sure to mark answered so it helps others Commented Nov 9, 2016 at 16:41
4

You need to mark the properties of the inner class as @AuraEnabled for them to be returned. This is likely why you are seeing a null return value (there is no visible data to return). Note that you may need to make your sets separate instead of inline as that has prevented me from adding the annotation before.

You must log in to answer this question.

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