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.