0

I need to customize lookup for custom object (fetch records applying some filter) on custom page.

What we have.

  1. Object ClientItem that contains object Operator. [ClientItem.Operator]

  2. Controller

     global with sharing class XCtrl {
        public Client_Item__c clientItem {get; set;}
        //........
        //........
    }
    
  3. Page

    < apex:page controller="XCtrl">< apex:form >
         < apex:inputField id="operatorText" required="false" value={!clientItem.Operator__c}" onchange="..."/>
     < /apex:form >< /apex:page >
    

So, i have lookup for input field. Is it possible to apply filter for it (fetch data with some criterias)

3

You can create your own custom lookup and can show records in custom lookup window as per your need or can create lookup filters at object level and can use that lookup on your custom page to show filtered records.

  • I tried to edit lookup on ClientItem obj page. But this filter is applied only when i create/update ClientItem , not on my page. – anastsiacrs Aug 13 '15 at 7:18
  • I pretty new in this sort of things, can you provide more details or maybe some link.. without creating custom lookup popup like here - blog.jeffdouglas.com/2011/08/12/… – anastsiacrs Aug 13 '15 at 7:21
  • I just recheck it by creating a custom filtered lookup and used that on VF page by using a controller in same way you have mentioned and it works fine. Please check your filter criteria and make sure you have created it properly and marked it Required instead of optional – Deepak Kumar Shyoran Aug 13 '15 at 7:39
  • Yes, you was right, thx. But I don't want this filter applied when ClientItem just created/updated... as i understand it's impossible and custom lookup page should be used int this case – anastsiacrs Aug 13 '15 at 8:15
0

You can use the below code and customize it as per your need. The below controller is used on the "Case" object VF page which has the lookup to "Contact" object. Here Contact lookup has been customized to see if a case is raised from "New" button in from "Case" tab then it shows list of all contacts. If case is raised from the related list in account then only those account specific contacts are shown. If case is raised from contact then the same records gets auto-populated in lookup.

========================================

Vf Page - CustomContactLookup


        <apex:outputPanel id="pnlSearchResults" style="margin:10px;height:350px;overflow-Y:auto;" layout="block">
            <apex:pageBlock id="searchResults"> 
              <apex:pageBlockTable value="{!results}" var="a" id="tblResults">
                <apex:column >
                  <apex:facet name="header">
                    <apex:outputPanel >Name</apex:outputPanel>
                  </apex:facet>
                   <apex:outputLink value="javascript:top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Name}', false)" rendered="{!NOT(ISNULL(a.Id))}">{!a.Name}</apex:outputLink>     
                </apex:column>
              </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:outputPanel>
    </apex:actionRegion>
 </apex:outputPanel>

Apex Class - CustomContactLookupController

public with sharing class CustomContactLookupController {

public List<Contact> results{get;set;} // search results
public String searchString{get;set;} // search keyword
public String recordId;
public String CustomerId;

public CustomContactLookupController() {
    // get the current search string
    recordId = '';
    if(System.currentPageReference().getParameters().get('recordId') != '')
        recordId = System.currentPageReference().getParameters().get('recordId');
        system.debug('!!!!!!!!!!!!!!recordId='+recordId);
        searchString = System.currentPageReference().getParameters().get('lksrch');
        system.debug('!!!!!!!!!!!!!!searchString='+searchString);
        runSearch();  
}

// performs the keyword search
public PageReference search() {
    runSearch();
    return null;
}

// prepare the query and issue the search command
private void runSearch() {
    // TODO prepare query string for complex serarches & prevent injections
    results = performSearch(searchString);
    system.debug('!!!!!!!!!!!!!!results='+results);               
}

// run the search and return the records found. 
private List<Contact> performSearch(string searchString) {
    String soql = 'select Id, Name from Contact';
    if(recordId != '' && recordId.startsWith('001')){
        if(searchString != '' && searchString != null)
        soql = soql +  ' where AccountId = \'' + recordId + '\' and Name LIKE \'%' + searchString +'%\'';
        else
        soql = soql +  ' where AccountId = \'' + recordId + '\'';
    } else if(recordId != '' && recordId.startsWith('003')){
        if(searchString != '' && searchString != null)
        soql = soql +  ' where Id = \'' + recordId + '\' and Name LIKE \'%' + searchString +'%\'';
        else
        soql = soql +  ' where Id = \'' + recordId + '\'';
    } else if(recordId != '' && recordId.startsWith('a1O')){
        Id accoundId = [select csord__Account__c from csord__Subscription__c where Id = :recordId].csord__Account__c;
        soql = soql + ' where AccountId = \'' + accoundId + '\'';
        if(searchString != '' && searchString != null)
        soql = soql + ' and Name LIKE \'%' + searchString +'%\'';
    }
    else {
        if(searchString != '' && searchString != null)
        soql = soql +  ' where Name LIKE \'%' + searchString +'%\'';
    }
    soql = soql + ' limit 25';
    System.debug(soql);
    return database.query(soql); 
}

// used by the visualforce page to send the link to the right dom element
public string getFormTag() {
    return System.currentPageReference().getParameters().get('frm');
}

// used by the visualforce page to send the link to the right dom element for the text box
public string getTextBox() {
    return System.currentPageReference().getParameters().get('txt');
}}

Below is the method how to put this in Case Custom VF page. Use this javascript in your parent object VF page.

// Following "003" is the lookup type for Contact object so change this as per your standard or custom object
    if(lookupType == "003"){

      var urlArr = baseURL.split("&");
      var txtId = '';
      if(urlArr.length > 2) {
        urlArr = urlArr[1].split('=');
        txtId = urlArr[1];
      }

      // Following is the url of Custom Lookup page. You need to change that accordingly
      baseURL = "/apex/CustomContactLookup?txt=" + txtId;

      // Following is the id of apex:form control "myForm". You need to change that accordingly
      baseURL = baseURL + "&frm=" + escapeUTF("{!$Component.myForm}");
      if (modified == '1') {
        baseURL = baseURL + "&lksearch=" + searchParam;
      }
      baseURL = baseURL + "&recordId=" + '{!recordId}';

      // Following is the ID of inputField that is the lookup to be customized as custom lookup
      if(txtId.indexOf('contact') > -1 ){
        isCustomLookup = true;
      }
    }

Hope this helps you.

Thanks!

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.