function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
svdsvd 

Look up filter issue in Visualforce

Hi,

 

I'm facing a lookup filter issue in Visualforce even though "inputfield" is used.

 

The following code works.

 

//

<apex:inputField value="{!c.ContactId}" required="true" />
<apex:inputField value="{!c.AccountId}" required="true" />

//

 

I have a lookup filter on Contact ID which fetches the contacts related to the selected account only. So c.AccountID is the controlling field. But when I make this c.AccountID an outputField with a value populated in Controller, my lookup filter on ContactID does not work.

 

Code that does not work.

 

//

<apex:inputField value="{!c.ContactId}" required="true" />
<apex:outputField value="{!c.AccountId}" required="true" />

// 

 

The reason I made c.AccountID as outputField is because, we don't want the user to update the AccountID in this case. It would be populated by System.

 

Please let me know how this can be fixed. I appreciate your time.

 

Thanks,
Svd

SeAlVaSeAlVa

I have a draft post on my blog waiting for me to finish it about this topic.

 

I will give you my "trick", I warned you that

  It is not pretty nice

  It does not avoid evil users changing the Account.ID from the source of the page.

 

Try replacing the line with the outputField of c.AccountID and use the following code.

 

<apex:pageBlockSectionItem >
  <apex:outputText>
    {!$ObjectType.Contact.fields.AccountID.label}
    <apex:outputPanel style="display:none">
      <apex:inputField value="{!c.AccountID}" />
    </apex:outputPanel>
  </apex:outputText>          
  <apex:outputField value="{!c.AccountID}"/>
</apex:pageBlockSectionItem>

 Regards.

SScholtzSScholtz

Sorry for the thread necromancy, but I was looking for a solution for this exact problem, and SeAIVa's solution worked well enough.  It might not be "clean" but it did the job!

 

---

 

To be complete, my scenario: I had a custom object, child to Account, which had a lookup to Contact. I wanted to filter the Contact look-up to show only Contacts connected to the Account, which was set on page load by a query string param.  The Account__c field would get set on load, but the Contact__c look-up wasn't "aware" of the id in order to provide context to the look-up filter unless the Account__c field was on the page as a inputField, and not outputField.