function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Janno RipJanno Rip 

Passing parameter from Lightning Component to Apex Class

Hi everyone,

this is probably a very common use case but I am struggling here. I want to make a query in apex based on a string I am sending from my component to the class. But it is not pulling any data. Here is what I got so far:

my ApexClass:
public class NextBestPotentialController {
@AuraEnabled
public static List<Lead> getLead (String JobTitle, Id recordId){
String searchTitle = '%' + JobTitle + '%';           
List<Lead> returnlead = new List<Lead>(); 
List<Lead>  myLeads = [SELECT Id,Name,JOB_Titel_letzte_bez_rel_Anzeige__c,company
        FROM Lead
        WHERE JOB_Titel_letzte_bez_rel_Anzeige__c LIKE :searchTitle AND Id != :recordId
        LIMIT 4
        
        ];
        
        for(Lead le:myLeads){
        returnlead.add(le);
        } 
        return returnlead ;      
    }   
   }

my Component:
<aura:component controller="NextBestPotentialController" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global">

<aura:attribute name="JobTitle" type="String"/>
<aura:attribute name="leadList" type="list" />
<aura:attribute name="recordId" type="Id" />

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


<div class="slds-grid slds-wrap">
<aura:iteration items="{!v.leadList}" var="lea"  >
    <div class="slds-col slds-size_1-of-4 slds-p-around_small">
<lightning:card title="Lead Information" footer="Sample footer">
    
    <p class="slds-p-horizontal_small">
     ID: {!lea.Id}
     </p>
    <p class="slds-p-horizontal_small">  
    Name: {!lea.Name}
    </p> 
    <p class="slds-p-horizontal_small">
     Firma: {!lea.Company}
     </p>
    <p class="slds-p-horizontal_small">
    Jobtitle: {!lea.JOB_Titel_letzte_bez_rel_Anzeige__c}
      </p> 

</lightning:card>
    </div> 


</aura:iteration>
</div>
  </aura:component>

my Controller:
 
({
 doInit : function(component, event) {
   
        var action  = component.get('c.getLead');
//  var JobTitle = component.get("v.JOB_Titel_letzte_bez_rel_Anzeige__c");
 
    action.setParams({
        
        recordId: component.get("v.recordId"),
        JobTitle: component.get("v.JOB_Titel_letzte_bez_rel_Anzeige__c")
       
    });
   
    action.setCallback(this,function(response){
        var state=response.getState();
        var response1=response.getReturnValue();
        if(state==="SUCCESS")
        {
            component.set("v.leadList",response1);
        }
       
    });
    $A.enqueueAction(action);
 }
})

The 'recordId' is getting passed to the class and working just fine. But lets say my 'Jobtitle' is 'developer' on my Lead record, nothing is getting passed and he is return leads where the 'Jobtitle' field is empty.

What do I need to do to make the controller understand that he should pull the 'Jobtitle' from the current lead and pass it to the class?!

I would like it to be generic to query leads based on the current leads jobtitle
ShirishaShirisha (Salesforce Developers) 
Hi Janno,

Greetings!

Seems like the issue is with the " WHERE JOB_Titel_letzte_bez_rel_Anzeige__c LIKE :searchTitle" in the SOQL query as it might not be able to get or understand the keywords entered.

Please enable the debug logs and check,if you are able to see any data on the variable "searchTitle" which should pass to the where Clause.

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Janno RipJanno Rip
Hello @Shirisha,

yes the problem ist that 'JobTitle' is null and I can't figure out how to populate it with the values for the current lead. If I set a default like this:

<aura:attribute name="JobTitle" type="String" default="developer"/>
and switch to

JobTitle: component.get("v.JobTitle")
it is working