Friday, July 1, 2011

Lookup Filter On Visaulforce Page in Salesforce


Requirement : If you want that if you select a Contact in Lookup Field , only that Account Parent should be papulate in next inputFiled/outputField lookup of Account.

We can do it something like this...

<apex:page controller="LookUpFilter" id="pageId">
 <script type="text/javascript">  
     function getAccount()
     {      
        ShowAccount();       
        return false;
     }             
 </script>
 <apex:form id="formid">
 <apex:messages />
 <apex:actionFunction name="ShowAccount" action="{!ShowAccount}" reRender="formid" status="actionStatusContactPrincipalRequired"/> 

 <apex:actionStatus startText="Processing.." id="actionStatusContactPrincipalRequired">
 </apex:actionStatus>

 <apex:pageBlock title="LookUpFilter" id="pb1">  
  <apex:pageBlockSection title="Auto Fill Account when select Contact">
         <apex:pageBlockSectionItem id="SecId">                         
             <apex:inputHidden value="{!HiddenField}" id="Cp_onpk"/>
             <apex:inputField value="{!std.Contact__c}" id="Cp"/>
         </apex:pageBlockSectionItem>
        
         <apex:pageblockSectionItem >
                   <apex:outputText value="Account"/>
                     <apex:outputField value="{!std.Account__c}"/> 
         </apex:pageblockSectionItem>             
     </apex:pageBlockSection>
 </apex:pageBlock>
 </apex:form> 
</apex:page>



public class LookUpFilter {
    public Account objAcc ;
    public Contact objCon ;
    public VJ__student__c std {get;set;}
    public string HiddenField {get;set;}
        
    public LookUpFilter(){
        HiddenField = 'getAccount();';            
        objAcc = new Account();
        objCon = new Contact();
        std = new VJ__student__c();
    }

    public void ShowAccount(){
    try{
        system.debug('For Test only:'+std.Account__c);
        if(std.Contact__c != null){
            Contact C = [select ID, AccountId from contact where ID = : std.Contact__c];
            system.debug('For Test only'+C);
            std.Account__c = C.AccountId;
        }
    }Catch(Exception ee){}
    }
  
}



Note : Make sure that the Ids field should be same as I have given _onpk , this is basically JS a library functions.


And If you want just Opposite of this requirement as if you select And Account and in next inputField of Contact should display only those contact which are related to this selected account then you can set a lookup filter using configuration ...
Just Click on Field Name - > Edit -> Insert Suggestion Creteria ->
Now set the filter creteria something like this :

Filter Criteria
Contact Name: Account Name IDequalsCase: Account Name ID

This is also have  a problem , when you click on Lookup Field on VF page then it will respect to filter creteria but you can see only recently viewed contact list whin in the filter creteria.


Thank.....Guys may this will help you ..

1 comment:

  1. I have 2 fields on my VF page. One is picklist and another one Lookup.
    I want to add filter criteria on lookup based on the picklist selected. I have already set field level criteria while creating lookup. Filter works while creating records from Salesforce UI, but not from VF page.

    ReplyDelete