0

I am trying to complete Apex Framework specialist. Challenge 3.

I have the following method in class

    public class BoatSearchResults {

    @AuraEnabled
    public static List<Boat__c> getBoats(String boatTypeId){

        List<Boat__c> boatList= null;

        if(boatTypeId != null && boatTypeId!= ''){
            try{
                boatList = [select BoatType__c, Contact__c, Description__c, Geolocation__c, Length__c, Picture__c, Price__c, Year_Built__c from Boat__c where BoatType__c =: boatTypeId];
            }

            catch(Exception e){
                System.debug('Error' +e);
            }
        }
        else{
            try{
                boatList = [select BoatType__c, Contact__c, Description__c, Geolocation__c, Length__c, Picture__c, Price__c, Year_Built__c from Boat__c];
            }
            catch(Exception ex){

                return null;
            }
        }
        System.debug('****'+boatList);
        return boatList;         
    }
}

BoatSearchResultcomponent.cmp

<aura:component controller="BoatSearchResults" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >

<aura:attribute name="boatTypeId" type="String" default=""/>
<aura:attribute name="boats" type="Boat__c[]"/>

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

BoatSearchResultComponentController.js

    ({
        doInit : function(component, event, helper) {

        helper.onSearch(component, event, helper);                
    },
   })

BoatSearchResultComponentHelper.js

  ({
        onSearch : function(component, event, helper) {

    var action = component.get("c.getBoats");

    action.setParams({
            boatTypeId : component.get("v.boatTypeId"),
    });

    action.setCallback(this, function(response){

        var state = response.getState();
        console.log('Is it error'+response.getState());
        if (state === "SUCCESS") {

            var resArr = response.getReturnValue(); 
            console.log('*****'+resArr);
            component.set("v.boats", resArr);                
        }
    });
    $A.enqueueAction(action);       
}
})

The response.getState() is continuously coming up as error. What am I doing wrong here.

3 Answers 3

1

response.getState() returns an ERROR if the server-side action fails to execute. Your code worked for me when I replaced Boat__c with a custom object of my own and just the Id and Name fields selected. Have you checked the debug logs? FYI - I noticed that catch statement in else condition is missing a debug statement.

Does select BoatType__c, Contact__c, Description__c, Geolocation__c, Length__c, Picture__c, Price__c, Year_Built__c from Boat__c where BoatType__c = '<boatTypeIdHere>' work for you in Query Editor in Developer Console?

6
  • Yes, the results are populating in the developer console, But the response request in js in coming as error. @Kal
    – Madhurima
    Jul 20, 2018 at 12:56
  • How about debug logs? Jul 20, 2018 at 13:03
  • I checked debug logs too. All the records are coming up in the query, with no issue. But still throwing a server-side error in js controller.
    – Madhurima
    Jul 20, 2018 at 13:06
  • That is highly unlikely. Are you sure you see the output of System.debug('****'+boatList); in debug logs? Do you see a value for boatList there? Jul 20, 2018 at 13:09
  • Yes I see a value for boatList, I am getting the following records 05:29:37.0 (4955569)|USER_DEBUG|[26]|DEBUG|****(Boat__c:{BoatType__c=a0K6F00000gSUUEUA4, Contact__c=0036F00002Y1aQbQAJ, Geolocation__c=API location: [38.875052 -77.049531], Length__c=20, Picture__c=/resource/Sailboats/sailboat1.png, Price__c=100000, Year_Built__c=2015, Id=a0L6F00001FE78wUAD}, Boat__c:{BoatType__c=a0K6F00000gSUUEUA4, Contact__c=0036F00002Y1aQcQAJ, Geolocation__c=API location: [39.282735 -76.6170849], Length__c=30, Picture__c=/resource/Sailboats/sailboat2.png, Price__c=500000, Year_Built__c=2013, ...... @Kal
    – Madhurima
    Jul 20, 2018 at 13:15
1

Apparently, it is because of the geo-location field. I have currently queried the individual components of the geo-location field separately. It is not throwing an error in js anymore.

select BoatType__c, Contact__c, Description__c, Geolocation__Latitude__s, Geolocation__Longitude__s, Length__c, Picture__c, Price__c, Year_Built__c from Boat__c

Don't know if there will be an issue when I have to actually use the geo-location field for any of the later challenges.

-1

In getboats apex method, change List<Boat__c> boatList= null; to List<Boat__c> boatList= new List<Boat__c> ();

2
  • It is still coming up as an error.
    – Madhurima
    Jul 20, 2018 at 12:30
  • 2
    List<Boat__c> boatList= null; is valid in this context because the apex method is directly assigning the result of a SOQL querying Boat__c to boatList. Jul 20, 2018 at 12:45

You must log in to answer this question.

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