-1

Built custom lookup referring below:

http://sfdcmonkey.com/2017/07/17/re-usable-custom-lookup/

Lookup field in Lightning components:

  public static List < sObject > fetchLookUpValues(String searchKeyWord, String ObjectName) {
    system.debug('ObjectName-->' + ObjectName);
    String searchKey = searchKeyWord + '%';
    List < sObject > returnList = new List < sObject > ();
    // Create a Dynamic SOQL Query For Fetch Record List with LIMIT 5  
    string sQuery = '';
    if(ObjectName == 'Queue'){
        sQuery = 'select Id,Name from Group where Type = \'Queue\' AND Name LIKE: searchKey order by createdDate DESC limit 5';  
    } 
    else if(ObjectName == 'Case'){

       sQuery =  'select id, CaseNumber from ' +ObjectName + 'where CaseNumber LIKE: searchKey order by createdDate DESC limit 5';   
    }
    else{
        sQuery =  'select id, Name from ' +ObjectName + ' where Name LIKE: searchKey order by createdDate DESC limit 5';    
    }

    List < sObject > lstOfRecords = Database.query(sQuery);
    for (sObject obj: lstOfRecords) {
        returnList.add(obj);
    }
    return returnList;
}

for queue and other objects it working properly. But Especially in case of case lookup that case field doesnt contain name field. so I used casenumber field for that

   sQuery =  'select id, CaseNumber from ' +ObjectName + 'where CaseNumber LIKE: searchKey order by createdDate DESC limit 5'; 

recieving below error:Unexpected token case Number

I belive case lookup should show case number(Autonumber).whats the best way to do that

But If I search it right it will not show any number in drop down:

enter image description here

0

This component assumes that the field returned is always Name. Since CaseNumber is different, we need to account for that in the JavaScript:

// We map CaseNumber to Name if present.
storeResponse.forEach(item => item.Name = item.Name || item.CaseNumber);
component.set("v.listOfSearchRecords", storeResponse);

Adding that one line of code should fix the display issue.

  • Thanks @sfdcfox – SFDC FAN Mar 13 at 2:16
2

The problem is with your else if block. You just need to add one more space before where keyword and your query will work fine.

    else if(ObjectName == 'Case'){

           sQuery =  'select id, CaseNumber from ' +ObjectName + ' where CaseNumber LIKE: searchKey order by createdDate DESC limit 5';   
        }
  • Okay.but If I search for number it will not display any mathching numbers in drop down – SFDC FAN Mar 12 at 7:24

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.