1

I have set up basic functionality in a Lightning Component to query my sandbox for the names of all opportunities on initialization and display them in a set of cards. When I attempt to preview the app, I get an internal server error. I tried the soql in Workbench and it returned a list of results so I'm not sure what the problem is.

Here is my code:

Opportunity List View Component

<aura:component controller="OpportunityListViewController">
      <aura:attribute name="opportunities" type="Opportunity" />
      <aura:handler name="init" action="{!c.doInit}" value="{!this}" />
      <c:cardList opportunities="{!v.opporunities}" />
</aura:component>

Opportunity List View Client Controller

    ({
    // Load opportunities from Salesforce
    doInit: function(component, event, helper) {

        // Create the action
        var action = component.get("c.getOpportunities");

        // Add callback behavior for when response is received
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (component.isValid() && state === "SUCCESS") {
                component.set("v.opportunities", response.getReturnValue());
            }
            else {
                console.log("Failed with state: " + state);
            }
        });

        // Send action off to be executed
        $A.enqueueAction(action);
    }
})

Opportunity List View Server Controller

public with sharing class OpportunityListViewController {
    @AuraEnabled
    public static List<Opportunity> getOpportunities() {
        return [SELECT Name
                FROM Opportunity];
    }
}
0

I've noticed when an exception is thrown in the related ApexController of the Lightning component we always get "Internal Server Error" error message. So I can suggest to surround your logic in Apex controller with try {} catch() and verify there are no errors in the server-side logic.

  • I added a try catch block and caught an error in the finally block but I'm unsure of how to get more information about the error. – Elise Rodrigues Sep 13 '16 at 12:49
  • What error you are getting now ? – MIX DML Sep 13 '16 at 13:42
0

Try returning a Wrapper object containing your opportunity list and some error information. An example:

public with sharing class OpportunityListViewController {
  @AuraEnabled
  public static OpportunityResult getOpportunities() {
    OpportunityResult result = new OpportunityResult();

    try {
      result.opportunities = [SELECT Name
            FROM Opportunity];
      result.success = true;
    }
    catch (Exception e){
      result.success = false;
      result.message = e.getMessage();
    }

    return result;

  }

  public class OpportunityResult{
    @AuraEnabled public Opportunity[] opportunities {get;set;}
    @AuraEnabled public Boolean success {get;set;}
    @AuraEnabled public String message {get;set;}

    public OpportunityResult(){
      opportunities = new Opportunity[]{};
    }
  }
}
  • How did you resolve it? – Caspar Harmer Sep 14 '16 at 20:33
  • I had a couple of misspelled attributes. – Elise Rodrigues Sep 15 '16 at 16:05

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.