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 {
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){
        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 class="slds-p-horizontal_small">  
    Name: {!lea.Name}
    <p class="slds-p-horizontal_small">
     Firma: {!lea.Company}
    <p class="slds-p-horizontal_small">
    Jobtitle: {!lea.JOB_Titel_letzte_bez_rel_Anzeige__c}



my Controller:

 doInit : function(component, event) {
        var action  = component.get('c.getLead');
//  var JobTitle = component.get("v.JOB_Titel_letzte_bez_rel_Anzeige__c");
        recordId: component.get("v.recordId"),
        JobTitle: component.get("v.JOB_Titel_letzte_bez_rel_Anzeige__c")
        var state=response.getState();
        var response1=response.getReturnValue();

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?!

enter image description here

Thanks in Advance!

1 Answer 1

  • You declared JobTitle attribute in component, but you are using JOB_Titel_letzte_bez_rel_Anzeige__c in controller.js. Make JobTitle attribute as a default value
  • If you want to make generic then you have to do SOQL to get the current Lead JOb Title, based on recordId that you are passing to the apex controller

To make the generic code...

// Get the current job title
String currentLeadJobTitle = [SELECT Id,JobTitle__c from Lead where Id =:recordId].JobTitle__c;

// Get all leads based on the job title
List<Lead> lstLeads = [SELECT Id,Name,JobTitle__c FROM Lead Where JobTitle__c =:currentLeadJobTitle];


<aura:attribute name="JobTitle" type="String" default="Developer"/>

    recordId: component.get("v.recordId"),
    JobTitle: component.get("v.JobTitle")

You must log in to answer this question.

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