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
cooltubcooltub 

Form with master-detail lookup field automatically populated with parameter id

Hi,

 

I'm facing a problem. I want the user to fill a form which has 3 fields name,email and a lookup(master detail field). The lookup field should be automatically populated with the parameter passed in URL. So, the user has to fill only 2 field with lookup field automatically populated and when he saves the record, it should be a detail record of the master record choosen. 

 

I'm in two minds here whether to choose detail as the controller or master as the controller. I tried to find somthing online which deals with filling a lookup field but in vain. Please can someone guide me here?

Best Answer chosen by Admin (Salesforce Developers) 
goabhigogoabhigo

Arrghh, use AccountId: <apex:inputField value="{!Contact.AccountId}"/>

 

Had thought about this, but to make sure asked your code.

All Answers

goabhigogoabhigo

So you are using a VF page right?

 

Use <apex:inputField> for the lookup field - automatically the field will be filled if you are being navigate to the current page through the master record, i.e. this page is opened as a result of clicking a button in related list of master record.

cooltubcooltub

Hi Abhi,

 

Yes this is a VisualForce page.I want the user to fill this form with lookup field automatically populated. When he saves the form it should be detail record of the Master record choosen.

goabhigogoabhigo

Sure. Write a visualforce page, standard controller would be detail object. Override the new button with this VF page ( Go to detail object - Standard Buttons and Links - Click on edit next to New button - choose this VF page.

 

In the VF markup, use <apex:inputField> for the master-detail relationship field. Automatically the master record will be populated in that field.

aka_floaka_flo

When I try to output the Account lookup field for a Contact, I get the following message:

 

" Expression value does not resolve to a field

Error is in expression '{!Contact.Account}' in component <apex:inputField> in page contactedit"


Did I miss something?  Why am I able to use <apex:inputfield value="{!Contact.Account}"/> for custom fields I created, but not for this standard SalesForce field?  Any insight would be greatly appreciated.

 

Thanks.

goabhigogoabhigo

Can you post the VF code?

aka_floaka_flo

This is what I have.  It doesn't matter, where I put that item though.  I get the same message.  Thoughts?

 

<apex:page standardController="Contact" sidebar="true">
    <apex:sectionHeader title="Contact Edit" subtitle="{!Contact.name}"/>
        <apex:form >
          <apex:pageBlock title="Contact Edit" id="thePageBlock" mode="edit">
            <apex:pageBlockButtons >
                    <apex:commandButton value="Save" action="{!save}"/>
                    <apex:commandButton value="Cancel" action="{!cancel}"/>                
            </apex:pageBlockButtons>
             <apex:actionRegion >
                <apex:pageBlockSection title="Information" columns="2">
                 <apex:inputField value="{!Contact.FirstName}"/>
                <apex:inputField value="{!Contact.LastName}"/>
                <apex:inputField value="{!Contact.Former_GE_Employee__c}"/>
                <apex:inputField value="{!Contact.Title}"/>

                <apex:inputField value="{!Contact.Contact_for_a__c}">
  <apex:actionSupport event="onchange" rerender="thePageBlock" status="status"/>
                </apex:inputField>
                  </apex:pageBlockSection>

                <apex:actionStatus startText="applying value..." id="status"/>
                              </apex:actionRegion>

                           <apex:pageBlockSection columns="2"
             rendered="{!Contact.Contact_for_a__c == 'Prospect'}">
                 <apex:inputfield value="{!Contact.Account}"/>
                 </apex:pageblocksection>
                           <apex:pageBlockSection columns="2"
             rendered="{!Contact.Contact_for_a__c == 'Association'}">
                 <apex:inputfield value="{!Contact.Association__c}"/>
                 </apex:pageblocksection>
                 <apex:pageBlockSection columns="2"
             rendered="{!Contact.Contact_for_a__c == 'Competitor'}">
                 <apex:inputfield value="{!Contact.Competitor__c}"/>
                 </apex:pageblocksection>
                 <apex:pageBlockSection columns="2"
             rendered="{!Contact.Contact_for_a__c == 'Consultant'}">
                 <apex:inputfield value="{!Contact.Consultant__c}"/>
                 </apex:pageblocksection>

                 </apex:pageBlock>
</apex:form>
</apex:page>

goabhigogoabhigo

Arrghh, use AccountId: <apex:inputField value="{!Contact.AccountId}"/>

 

Had thought about this, but to make sure asked your code.

This was selected as the best answer
aka_floaka_flo

This worked!  Thanks for your help!