0

This question already has an answer here:

This is related to Lightning component framework. Though, I have passed challenge (till 4), however result is not as expected. I am passing boatTypeId as a parameter from helper-onSearch to apex method-getBoats(). getBoats() return list of boats having the same boatTypeId. Below is my helper and the apex class.

({
    onSearch : function(component, event, helper) {
       // console.log('In Helper');
        var action=component.get('c.getBoats');
        var btypeId =component.get("v.boatTypeId");
        console.log('BoatSearchResult helper boattypeId '+btypeId);
        action.setParam({"boatTypeId" :btypeId});
        action.setCallback(this,function(response) {
            if(response.getState() === 'SUCCESS')
            {
                console.log('response from server'+ response.getReturnValue());
                var boatslist = response.getReturnValue();
                console.log('Number of Boats returned'+boatslist.length);
                component.set("v.boats",boatslist);
            } 
            else
            {
                console.log('Error caught in !!!');
            }
        });
        $A.enqueueAction(action);
    }
})

public with sharing class BoatSearchResults {

    @Auraenabled
    public static List<Boat__c> getBoats(string boatTypeId)
    {
       if(boatTypeId != '')
       {
           system.debug(boatTypeId);
           system.debug('Hello again');
           return ([Select Id, BoatType__c , Contact__r.Name, Name, picture__c from Boat__c Where BoatType__c = :boatTypeId]); 
          // return ([SELECT Id, BoatType__c, picture__c, Name,contact__r.Name from Boat__c]);     
       }
       else
       {
           system.debug('Hello again2');
           return ([SELECT Id, BoatType__c, picture__c, Name,contact__r.Name from Boat__c]);

       }
    }
}

Couple of issues here, I find here:

  1. boatTypeId passed in the getBoats() is always null

  2. If boatTypeId is null , then else part of getBoats() should be executed, but if part of the method is executed all the times. (Maybe I am missing basics of SOQL here)

Can someone point me in the right direction?

marked as duplicate by glls, David Reed, Raul, Dave Humm, Oleksandr Berehovskyi Apr 17 '18 at 11:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    system.debug(boatTypeId); is null? did you try passing your parameter with "action.setParams" instead of "action.setParam" ? – glls Apr 16 '18 at 14:02
  • It's best to use String.isNotBlank() instead of the boatTypeId != ''. This will check if the boatTypeId is null, empty or just whitespace chars. More documentation here: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…! With regards to the parameter passing, @glls is right, use action.setParams instead. – Rohit Kunal Apr 16 '18 at 14:22
  • Using action.setParams and String.isNotBlank solved the issue. However, not sure why the boatTypeId != '' (comparison operator to null) , wouldn't work ? – Manish Anand Apr 16 '18 at 14:58
  • 1
    An empty string is not null (it's a string with a length of zero). – sfdcfox Apr 16 '18 at 15:24
2

action.setParam({"boatTypeId" :btypeId}); here setParam is used to set single paramter and you are passing an objet which is not correct. So to make it work either change it to action.setParams({"boatTypeId" :btypeId}); OR action.setParam("boatTypeId", btypeId);

setParam and setParams

More you can find on the Aura Documentations site.

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