I have a dropdown in my lightning component that calls an apex method on the backend. There are two options in this dropdown. For one, everything works as is expected. For the other option, I have stepped through the controller, helper, and apex class and everything is as expected until I return the object to my callback function. At this point, absolutely nothing is returned to the callback function.
Here is my js controller:
getAccountSummary: function(component, selectedLocation){
var action = component.get("c.getAccountSummaryApex");
action.setParams({
"selectedLocation": selectedLocation,
});
action.setCallback(this, function(response) {
alert('response = ' + response);
var state = response.getState();
if (component.isValid() && state == "SUCCESS") {
var result = response.getReturnValue();
component.set("v.accountSummary", result);
}
});
// Send action off to be executed
alert('called apex');
$A.enqueueAction(action);
},
Here is my apex method:
@AuraEnabled
public static AccountSummary getAccountSummaryApex(String selectedLocation{
User currentUser = getCurrentUser();
List<Account> relatedAccounts = getRelatedAccount(currentUser, null);
List<Zuora__CustomerAccount__c> billingAccounts =
getBillingAccounts(relatedAccounts, false);
List<Zuora__ZInvoice__c> totalInvoiceList = new List<Zuora__ZInvoice__c>();
totalInvoiceList = [SELECT id, Name, Zuora__Zuora_Id__c, Zuora__BillingAccount__r.Zuora__Zuora_Id__c, Zuora__Account__c,
Zuora__Account__r.Payment_Method__c, Zuora__Account__r.Location2__c,
Zuora__Account__r.Location2__r.Name, Payment_Status__c, Zuora__Payment_Term__c, Zuora__DueDate__c,
Zuora__Balance2__c, Outstanding_amount_before_credit__c, Zuora__CreditBalanceAdjustmentAmount__c,
Zuora__Description__c, Zuora__TotalAmount__c
FROM Zuora__ZInvoice__c
WHERE Zuora__Account__c IN :relatedAccounts AND Zuora__Account__r.Location2__c =: selectedLocation
AND Payment_Status__c != :paymentStatusProcessing AND Zuora__Balance2__c > 0];
AccountSummary acctSum = new AccountSummary();
if(!totalInvoiceList.isEmpty())
{
for (Zuora__ZInvoice__c invoice:totalInvoiceList) {
if (invoice.Zuora__DueDate__c < Date.today() && invoice.Outstanding_amount_before_credit__c != null &&
invoice.Zuora__CreditBalanceAdjustmentAmount__c != null && invoice.Zuora__Balance2__c != null) {
acctSum.previousBalance = acctSum.previousBalance + invoice.Outstanding_amount_before_credit__c;
acctSum.creditApplied = acctSum.creditApplied + invoice.Zuora__CreditBalanceAdjustmentAmount__c;
acctSum.totalBalanceDue = acctSum.totalBalanceDue + invoice.Zuora__Balance2__c;
} else if (invoice.Zuora__DueDate__c >= Date.today() && invoice.Outstanding_amount_before_credit__c != null &&
invoice.Zuora__CreditBalanceAdjustmentAmount__c != null && invoice.Zuora__Balance2__c != null){
acctSum.newCharge = acctSum.newCharge + invoice.Outstanding_amount_before_credit__c;
acctSum.creditApplied = acctSum.creditApplied + invoice.Zuora__CreditBalanceAdjustmentAmount__c;
acctSum.totalBalanceDue = acctSum.totalBalanceDue + invoice.Zuora__Balance2__c;
}
}
}
system.debug('return accountSummary = ' + acctSum);
return acctSum;
}
The system.debug contains the expected value for either option. Is there any reason why 'acctSum' wouldnt get returned correctly after the value is correct on the line before.
Also, acctSum is of type AccountSummary, which can be seen below.
public class AccountSummary {
public AccountSummary(){
this.previousBalance = 0.0;
this.newCharge = 0.0;
this.creditApplied = 0.0;
this.totalBalanceDue = 0.0;
}
@AuraEnabled
public double previousBalance{get;set;}
@AuraEnabled
public double newCharge{get;set;}
@AuraEnabled
public double creditApplied{get;set;}
@AuraEnabled
public double totalBalanceDue{get;set;}
}
Best Answer
Try changing your controller method from
alert('response = ' + response);
toconsole.info('response', response);
to see what you are actually getting from the server. (don't forget to open your browser console)If you have just created your component today, chances are that you are using Lightning API 40.0, which has a much more secure window, which may be causing your code to fail silently (possibly your alert).
I would suggest turning on lightning debug mode, as well. Setup -> Lightning Components -> Click the checkbox to enable debug for lightning components, and click Save
This allows for many of the silent js failures in lightning to bubble up for you to see.