2

While doing the super badge  "Lightning Component Framework Specialist" I get the Internal Server error.

here is the code snippet.

BoatSearchResults.apxc

public with sharing class BoatSearchResults {
 @AuraEnabled
    public static List<Boat__c> getBoats(String boatTypeID) {
        System.debug('BoatSearch Results : ' + boatTypeID);
        List<Boat__c> returnList;
            if (boatTypeID != ''){
                returnList = [Select ID, Name, BoatType__c, contact__r.Name, Geolocation__c, Picture__c from Boat__c
                       where BoatType__c =:boatTypeID];
            } else {
                returnList = [Select ID, Name, BoatType__c, contact__r.Name, Geolocation__c, Picture__c from Boat__c];
            }
        System.debug('BoatSearch Results Return Count: ' + returnList.size());
        return returnList;
    }
}

BoatSearchResultsHelper.js

({
 onSearch : function(component) {
        // Create the action
        var boatTypeID = component.get('v.selectedBoatID'); 
  console.log('In onSearch function: ' + boatTypeID);
        var action = component.get("c.getBoats");
        action.setParams({boatTypeID : boatTypeID});
        // Add callback behavior for when response is received
        action.setCallback(this, function(response) {
            var state = response.getState();
   console.log('In onSearch function state: ' + state);
            if (state === "SUCCESS") {
                component.set("v.boats", response.getReturnValue());
            }
            else {
          console.log('In onSearch function Failed: ' + response);
                console.info('RESPONSE', response);
               var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("***Error message: " +
                                 errors[0].message);
                    }
                 } else {
                    console.log("***Unknown error");
                }
                console.log("Failed with state: " + state); 
            }
        });
       
        // Send action off to be executed
        $A.enqueueAction(action);
 }
})

BoatSearchResults.cmp

<aura:component implements="flexipage:availableForAllPageTypes" controller="BoatSearchResults">
    <aura:handler name="boatSelect" event="c:BoatSelect" action="{!c.handleBoatSelect}"/>
   
    <aura:method name="search"
      description="Method to search all boats related to boat type">
        <aura:attribute name="boatTypeId" type="String" />
    </aura:method>
   
   
    <aura:attribute name="selectedBoatID" type="String"/>
    <aura:attribute name="boats" type="Boat__c[]" default="[]"/>
   
 <lightning:layout horizontalAlign="space" multipleRows="true">
        <aura:if isTrue="{!not(empty(v.boats))}">
            <!--Iterate the mapEntry Component.-->
            <aura:iteration items="{!v.boats}" var="boat" >
                <lightning:layoutItem flexibility="auto" size="12" mediumDeviceSize="6" largeDeviceSize="3" padding="around-small">
                 <c:BoatTile boat="{!boat}" selected="false"/>
                </lightning:layoutItem>
            </aura:iteration>
            <aura:set attribute="else">
                <div class="slds-align_absolute-center" style="text-align:center;"> No boats found </div>
            </aura:set>
        </aura:if>
    </lightning:layout>
</aura:component>

Strangely  I did clear the step 4 “Implement the search filter” even with this error. However I want this to be resolved so that I can go ahead with other steps.

Any help will be appreciated .

7
  • Basically this error comes up in Markup(at least for me). Try rolling back the content inside lightning:layout and see if the error goes away. I think it has to do with how you use lightning:layoutItem. let us know.
    – SfdcBat
    Dec 9, 2017 at 16:31
  • I have commented the whole layout section, still I get the error. Dec 10, 2017 at 1:35
  • @SfdcBat thanks you for your response. I looked at the logs. I find there is no issue at server side, logs show the right values. I see error happening at "var state = response.getState(); console.log('In onSearch function state: ' + state);" Dec 10, 2017 at 1:38
  • Your SOQL seems to be not correct. boatTypeID is the name of the boat type. So the SOQL should be - SELECT Id, Name,Contact__r.name FROM Boat__c where BoatType__r.Name=:boatTypeId. When you cleared challenge 4 , did you see the boats appearing on the page, if not something is breaking in other components may be - BoatSearch / BoatSearchForm. Also I used the content of your markup BoatSearchResults ( the content from lightning:layout) and it did not allow me to save the cmp. It showed some error. Not sure how you got it saved.
    – SfdcBat
    Dec 10, 2017 at 4:16
  • @SfdcBat yes I found that there is some problem with Geoloaction. I saw error in console log. Once I removed GeoLoaction no error was reported from server side. Now resolving problem related to layout. No, I don't see any search results due to the problems. Clearing challenge 4 was by accident. I clicked on "Check Challenge" and it was successful. Something that Salesforce Trailhead team should look into. Dec 10, 2017 at 8:01

3 Answers 3

6

The answer is in the comments, however, here are the details of the error. It looks like there is a problem with serializing Apex objects to JSON.

Calling server controller method, I get the following response:

while(1); {"actions":[{"id":"578;a","state":"SUCCESS","returnValue":[{"BoatType__c":"a011N00000ULFDXQA5","Contact__c":"0031N00001J1joRQAR","CreatedById":"0051N000005NFw3QAG","CreatedDate":"2017-12-15T01:07:09.000Z","Geolocation__c":*/{"message":"An internal server error has occurred\nError ID: 606597035-115135 (-1865483868)"}/ERROR/

The State flag is set to 'SUCCESS' and we even see Boat__c object properties of the first record. As soon as we get to Geolocation__c we get a server error.

Looking at the data returned, Geolocation__c is an object.

enter image description here

So It looks like either Salesforce is having issues serializing it or we have to do something special to handle these types of objects...

In the interest of saving time, and because Geolocation__c field is not required, we can "fix" the problem by removing Geolocation__c from the query but it would be nice if this was resolved somehow.

1

Salesforce cannot return a result with a Location field in it.

But the Apex controller could return a JSON String:

public with sharing class BoatSearchResults {

    @AuraEnabled
    public static String getBoats(String boatTypeID) {

        List<Boat__c> returnList;

        if (boatTypeID != ''){
            returnList = [Select ID, Name, BoatType__c, contact__r.Name, Geolocation__c, Picture__c from Boat__c where BoatType__c =:boatTypeID];
        } else {
            returnList = [Select ID, Name, BoatType__c, contact__r.Name, Geolocation__c, Picture__c from Boat__c];
        }

        return JSON.serialize(returnList, true);
    }
}

And the you could use JSON.parse to convert the string to an object in JavaScript:

var jsonObject = JSON.parse(response.getReturnValue());
0

Salesforce cannot return an SObject with a Location field (which is a compound field -- type is Schema.DisplayType.Location) from Apex to Aura or Lightning Web Components. It can, however, return the components of the compound field (which are of type Schema.DisplayType.Double). Note that I'm unable to find this explicitly stated in documentation, but have verified through testing.

Assume a Geolocation field called Geo__c on the Account object. If you are hard-coding the fields, you can simply do this:

return [SELECT Id, Geo__Latitude__s, Geo__Longitude__s FROM Account];

Including Geo__c as a SELECT field will cause an "internal server error" when the response is sent back to the Aura Component or LWC. Note that the Apex transaction itself will be successful, since the failure occurs after the transaction completes and the response is sent back to the front-end (for example, if you're updating a record, the transaction can be successful even if the error shows up on the front end).

If you are building a query dynamically, you'll want to use Schema methods to get the DescribeFieldResult for Geo__c:

List<String> selectFields = new List<String>();
String apiName = 'Geo__c`; // Probably would be passed in as a parameter
SObjectField field = Account.SObjectType.getDescribe().fields.getMap().get(apiName);
DescribeFieldResult fieldDescribe = field.getDescribe(); // add null checks as appropriate
if (fieldDescribe.getType() == DisplayType.Location) {
    String fieldBase = apiName.removeEndIgnoreCase('c');
    selectFields.add(fieldBase + 'latitude__s'); // Geo__latitude__s
    selectFields.add(fieldBase + 'longitude__s'); // Geo__longitude__s
} else {
    selectFields.add(apiName);
}

You must log in to answer this question.

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