0

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

enter image description here

Thanks in Advance!

1 Answer 1

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];

Ex:

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

action.setParams({
    recordId: component.get("v.recordId"),
    JobTitle: component.get("v.JobTitle")
});
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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