+ Start a Discussion

How to set the text value in lookup field



I would like to create a visual force page, which contains lookup fields.


And I would like to set some basic value in lookup field text box. So that user can click the "lookup" button to query with some basic condition.


For example:
<inputField value="a"/> (text field)
<inputField value="b"/> (lookup field)


User will set a value first, maybe "company", then I would like to copy the value of a ("company") to b's text box, so that I can query b with conditon of a ("company").


Any one can help me?





You don't have access to b's text value in Apex Code, only its ID, if any. You could accomplish your task using JavaScript, if you were so inclined, however.


<apex:page standardController="Contact">
    function updateAccountId(e) {
        var accountId = document.getElementById('{!$Component.form.accountId}');
        accountId.value = e.value;
    <apex:form id="form">
        <apex:inputText onchange="updateAccountId(this);" value="{!Contact.Description}"/>
        <apex:inputField id="accountId" value="{!Contact.AccountId}"/>

Of course, change your values to suit, this is only for demonstration purposes. Note that you still can't access the text value of accountId in Apex Code, but if there is a reRender condition somewhere, the system would helpfully auto-fill with an ID value or a list of options if there are multiple choices.

Naveen NelavelliNaveen Nelavelli

one way solution is javascript..which is explained already another way is to use workflow to update b lookupfield...