0

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;}

}
  • Even if both of your if statements evaluate as false, I would still expect that you would get an AccountSummary object back populated with zeros... Any chance your Apex Class is throwing an exception?? – John Thompson Sep 26 '17 at 23:44
  • @mrchris console.log(result) doesn't return anything in your browser console? – glls Sep 27 '17 at 0:43
  • Are you checking your system.debug when you run your component? Or when you're running tests in Apex? If you're not getting any kind of response in your javascript callback, then chances are there's an exception occurring in apex before it can return the AccountSummary object. – Jason Benkert Sep 27 '17 at 0:52
  • You don't have an error block in your callback: else if (component.isValid() && state == 'ERROR') { console.log(t.getError()[0].message)); so you can at least log any errors. Does the alert show the appropriate response? Have you put breakpoint in the console to step through the code? – Eric Sep 27 '17 at 3:05
1

Try changing your controller method from alert('response = ' + response); to console.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.

  • Lightning Debug Mode worked well. Was able to find an error in a separate JS function that was causing the issue. Thanks for the tips! – mrchris Sep 27 '17 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.