0

In Salesforce, there is a out of box filter functionality (lookup filter) for lookup input field. I need the SAME functionality in autocomplete box. Out of the box functionality: field definition (image 1 - green box), example (image 2 - red box).

Currently, I am using this component as AUTOCOMPLETE lookup field but I am open to use another if I get the lookup filter function. In this autocomplete feature, I can refer source SObject for lookup. But this pulls all records of that sObject (not the one with lookup filter). I am using Visualforce page, & I don't wanna use standard lookup field (my requirement is to use aucomplete feature). Current page (image 3) is pulling all records but I only need to pull filtered 3 records (left side of 2nd image - red box). The important VF line of codes is given below (c:TypeAheadComponentMC is the component from blogForce9):

<apex:pageBlockSection columns="1" > <!-- GLA_filtered_Category -->
   Category  <br/>
   <apex:pageBlockSectionItem >     <c:TypeAheadComponentMC allowClear="true" importJquery="true" labelField="name" SObject="General_Ledge_Account__c" inputFieldId="00NN00000027Y78" valueField="Id" targetField="{!objJour.General_Ledger_Account__c}" style="width:95%; margin:0; border-radius:1rem;"  /> </apex:pageBlockSectionItem>
   Person   <br/>
   <apex:pageBlockSectionItem >    <c:TypeAheadComponentMC allowClear="true" importJquery="true" labelField="Name" SObject="Account" valueField="Id" targetField="{!objJour.Account__c}" style="width:95%; margin:0; border-radius:1rem;"  /> </apex:pageBlockSectionItem>
</apex:pageBlockSection>

green 2 VF page

1
  • I don't think your custom component is support this. You need to write custom code for this and modify this component. Dec 26, 2016 at 6:27

1 Answer 1

2

I just looked at the package you used. In this package, there is a controller which is used to pass the records to the page for autocomplete to use. The class name is: AutoCompleteV2_Con. In this class, they have a function where they collect the records pass it to the inner class. You have to make changes in this function. Let say, for eg, you have to filter based on the Account Name. Only Accounts whose name starts with test needs to be displayed. Then, in the function, you have to do something like,

 for( Sobject sObj : Database.query('SELECT '+valueFieldVar+','+labelFieldVar+' FROM '+sObjVal+' WHERE '+labelFieldVar+' LIKE \'%test%\'')){
                AutoCompleteDatas.add(new AutoCompleteData(sObj.get(valueFieldVar),sObj.get(labelFieldVar)));
            }

If you want to add further filter, you need to change the query specified in the for loop. You can also build your own parameters to pass to the query as per your requirements. /****UPDATE****** @Tanmoy: You have to change the getData function. You have to first check if the sObject passed is the General Account and pass the additional filter criteria and construct the query string. Then use the query string in for loop. Something like this:

@RemoteAction
    public static List<AutoCompleteData> getData(String sObjVal,String labelFieldVar,String valueFieldVar,String param){

        List<AutoCompleteData> AutoCompleteDatas = new List<AutoCompleteData>();
        param = String.escapeSingleQuotes(param);
        String queryToPass = 'SELECT '+valueFieldVar+','+labelFieldVar+' FROM '+sObjVal+' WHERE '+labelFieldVar+' LIKE \'%'+param+'%\'';
        if(sObjVal == 'General_Ledger_Account__c')
            queryToPass = queryToPass + ' AND Operational_Category__c = true';
        for( Sobject sObj : Database.query(queryToPass)){
            AutoCompleteDatas.add(new AutoCompleteData(sObj.get(valueFieldVar),sObj.get(labelFieldVar)));
        }

        return AutoCompleteDatas;

    }
4
  • I need the following SOQL query to embed inside component code, where & how to put this? List<General_ledge_Account__c> GLA_filtered_Category = [SELECT id, name FROM General_ledge_Account__c WHERE Operational_Category__c = true];
    – Tanmoy Das
    Dec 26, 2016 at 8:38
  • I have implemented the SOQL, & AUTOCOMPLETE box is displaying result perfectly. -------------------------- --------------- for( Sobject sObj : Database.query('SELECT '+valueFieldVar+','+labelFieldVar+' FROM '+sObjVal+' WHERE '+labelFieldVar+' LIKE \'%'+param+'%\' AND Operational_Category__c = true ')) -------------- But, I need to ADD another "CustomField__c = true" condition for another INPUT field. If I add "CustomField__c = true" in the same for loop, it is combining the condition (it is supposed to), but I need 2 different conditions for 2 different INPUT Autocomplete field.
    – Tanmoy Das
    Dec 26, 2016 at 9:18
  • @tanmoy: For that, you need to first check the sObject type as I have added in the above answer(Just updated it) and construct your filter query accordingly. Hope this helps! Dec 26, 2016 at 9:20
  • 2 different input fields which are linked to the same CustomObject__c
    – Tanmoy Das
    Dec 26, 2016 at 9:49

You must log in to answer this question.

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