+ Start a Discussion
Dave BerenatoDave Berenato 

Prepopulate Lookup Field in Standard Controller Visualforce Page

I have a Visualforce Page to make a new record of a custom object we call "Notes"
<apex:page standardController="Note__c" showHeader="false">
    <apex:form id="theForm">
        <table>
            <tr>
                <th rowspan="2">Note Type</th>
                <th>Note Name</th>
                <th>Note Body</th>
                <th rowspan="2"><apex:commandButton value="Save" action="{!Save}" rerender="theForm" /></th>
            </tr>
            <tr>
                
                <th><apex:inputField value="{!Note__c.Name}"/></th>
                <th><apex:inputField value="{!Note__c.Content__c}"/></th>
            </tr>
        </table>
    </apex:form>
</apex:page>

I want to display a link to the URL off the Account, which is the Parent object in a Master-Detail Relationship field (Account__c).

Can I preopulate the Lookup Field with this value using a URL from the Account?

I've tried:

1. 
https://corere--c.na50.visual.force.com/apex/NewNoteiFrame?Account__c=0016A000001p8GD
2.
https://corere--c.na50.visual.force.com/apex/NewNoteiFrame?Account__r.Id=0016A000001p8GD
3.
https://corere--c.na50.visual.force.com/apex/NewNoteiFrame?00N6A000008PlNb=0016A000001p8GD
4.
https://corere--c.na50.visual.force.com/apex/NewNoteiFrame?Account__c=12345+Test+Drive

But none of them work. Any ideas?


 
Best Answer chosen by Dave Berenato
Dave BerenatoDave Berenato
Okay, no problem. For anyone who finds this thread the below code worked:
 
public class NoteExtensionCtrl {
 
    Note__c t;

    private ApexPages.StandardController stdCtrl{get;set;}
    //Public Id Id = ApexPages.currentPage().getParameters().get('id');
    public NoteExtensionCtrl(ApexPages.StandardController std) {
        stdCtrl = std;
        Note__c note = new Note__c();
        note = (Note__c) stdCtrl.getRecord();  
        note.Account__c = ApexPages.currentPage().getParameters().get('AccountId');    
    }
}
<apex:page standardController="Note__c" extensions="NoteExtensionCtrl" showHeader="false">
    <apex:form id="theForm">
        <table>
            <tr>
                <th>Note Name</th>
                <th>Note Body</th>
                <th rowspan="2"><apex:commandButton value="Save" action="{!Save}" rerender="theForm" /></th>
            </tr>
            <tr>
                
                <th><apex:inputField value="{!Note__c.Name}"/></th>
                <th><apex:inputField value="{!Note__c.Content__c}"/></th>
            </tr>
        </table>
    </apex:form>
</apex:page>


 

All Answers

Raj VakatiRaj Vakati
You no need to populate explicitly . when you pass the note__c record Id to the URL it will populate the data  
/apex/NewNoteiFrame?Id=<Note__C Id>
PawanKumarPawanKumar
you would need to write Controller extension and read the parameter in controller constructor and set the RelationshipField=AccountId then it will work.

https://corere--c.na50.visual.force.com/apex/NewNoteiFrame?AccountId=00N6A000008PlNb

Regards,
Pawan Kumar
 
Abdul KhatriAbdul Khatri
I made a small tweak in your page
 
<apex:page standardController="Note__c" showHeader="false">
    <apex:form id="theForm">
        <table>
            <tr>
                <th rowspan="2">Note Type</th>
                <th> Account </th>
                <th>Note Name</th>
                <th>Note Body</th>
                <th rowspan="2"><apex:commandButton value="Save" action="{!Save}" rerender="theForm" /></th>
            </tr>
            <tr>
                <th><apex:outputText value="{!$CurrentPage.parameters.Account__c}"></apex:outputText></th>
                <th><apex:inputField value="{!Note__c.Name}"/></th>
                <th><apex:inputField value="{!Note__c.Content__c}"/></th>
            </tr>
        </table>
    </apex:form>
</apex:page>

User this URL
https://corere--c.na50.visual.force.com/apex/NewNoteiFrame?Account__c=0016A000001p8GD

This will prepopulate the Account field. Not sure is that what you wanted because I don't see which lookup field you wanted to populate.
Dave BerenatoDave Berenato
Hi Abdul,

This is close. I am trying to populate the Lookup field "Account__c" which is why I called the parameter "Account__c" but how to do I put that Parameter into the InputField?
 
<apex:page standardController="Note__c" showHeader="false">
    <apex:form id="theForm">
        <table>
            <tr>
                <th colspan="2">Note Account</th>
                <th>Note Name</th>
                <th>Note Body</th>
                <th rowspan="2"><apex:commandButton value="Save" action="{!Save}"/></th>
            </tr>
            <tr>
                <td><apex:outputText value="{!$CurrentPage.parameters.Account__c}"/></td>
                <td><apex:inputField value="{!Note__c.Account__c}"/></td>
                <td><apex:inputField value="{!Note__c.Name}"/></td>
                <td><apex:inputField value="{!Note__c.Content__c}"/></td>
            </tr>
        </table>
    </apex:form>
</apex:page>

In this case, youwould use URL:

​https://corere--c.na50.visual.force.com/apex/NewNoteiFrame?Account__c=12345+Test+Drive

And then you would have to copy and paste the output Text from the first cell into the inputField Note__c.Account__c. Is there any way it could show this parameter inside the inputField on load?
Abdul KhatriAbdul Khatri
Not sure why you wanted to do it like that. Since you already have Master Detail Relationship. There is an out of box solution for this no need for the Visualforce Page.

Navigate to any Account. You should be able to see the Related List as shown below
User-added image

You can navigate to any account like 

https://corere--c.na50.visual.force.com/0016A000001p8GD (https://corere--c.na50.visual.force.com/apex/NewNoteiFrame?Account__c=0016A000001p8GD)

If it doesn't show, you need to update the layout and add the Related List.

The New button there will automatically populate the Account and you just need to update the other two fields. Why to complicate life when out of box solution available

User-added image
Dave BerenatoDave Berenato
Hi Abdul,

I know the out of the box functionality for new Related Records is one solution. I'm actually displaying this page on another Visualforce page, so I won't have access to the New Note button the Account record, I only have the account ID or name to plug into this Visualforce page to make a new note. Is there any way to do like:
 
<apex:inputField field={!Note__c.Account__c}" value="{!$CurrentPage.parameters.Account__c}">

 
PawanKumarPawanKumar
Please try below code:

VF:
---------------
<apex:page standardController="Note__c" extensions="NoteExtensionCtrl" showHeader="false">
    <apex:form id="theForm">
        <table>
            <tr>
                <th rowspan="2">Note Type</th>
                <th>Note Name</th>
                <th>Note Body</th>
                <th rowspan="2"><apex:commandButton value="Save" action="{!Save}" rerender="theForm" /></th>
            </tr>
            <tr>
                
                <th><apex:inputField value="{!Note__c.Name}"/></th>
                <th><apex:inputField value="{!Note__c.Content__c}"/></th>
            </tr>
        </table>
    </apex:form>
</apex:page>
--------------------------

Controller
---------------------
public class NoteExtensionCtrl{

    private ApexPages.StandardController stdCtrl{get;set;}
    //Public Id Id = ApexPages.currentPage().getParameters().get('id');
    public NoteExtensionCtrl(ApexPages.StandardController std) {
        stdCtrl = std;
        note = (Note__c) stdCtrl.getRecord();  
        note.Account__c = ApexPages.currentPage().getParameters().get('AccountId');
    }
       
}
-----------------

URL:
https://corere--c.na50.visual.force.com/apex/NewNoteiFrame?AccountId=00N6A000008PlNb

Please let me know in case of any further issue.

Regards,
Pawan Kumar
 
Abdul KhatriAbdul Khatri
You can go with pawanKumar Solution then
PawanKumarPawanKumar
Hi Dave,

if it works for you then please close this thread. Thanks.

Regards,
Pawan Kumar
Dave BerenatoDave Berenato
Hi Pawan,

You forgot to state the Note variable as a new Note, but that works perfectly, thank you!
 
public class NoteExtensionCtrl {
 
    Note__c t;

    private ApexPages.StandardController stdCtrl{get;set;}
    //Public Id Id = ApexPages.currentPage().getParameters().get('id');
    public NoteExtensionCtrl(ApexPages.StandardController std) {
        stdCtrl = std;
        Note__c note = new Note__c();
        note = (Note__c) stdCtrl.getRecord();  
        note.Account__c = ApexPages.currentPage().getParameters().get('AccountId');    
    }
}

 
Dave BerenatoDave Berenato
If you post the correct code with the Visualforce I'll mark it as the best answer, I don't want to confuse anyone.
PawanKumarPawanKumar
only Idea/solution matters in the universe. BTW, Good to know proposed idea/solution working for you. Thanks.
Also, thanks to Abdul for sharing OOB solution.
Dave BerenatoDave Berenato
Okay, no problem. For anyone who finds this thread the below code worked:
 
public class NoteExtensionCtrl {
 
    Note__c t;

    private ApexPages.StandardController stdCtrl{get;set;}
    //Public Id Id = ApexPages.currentPage().getParameters().get('id');
    public NoteExtensionCtrl(ApexPages.StandardController std) {
        stdCtrl = std;
        Note__c note = new Note__c();
        note = (Note__c) stdCtrl.getRecord();  
        note.Account__c = ApexPages.currentPage().getParameters().get('AccountId');    
    }
}
<apex:page standardController="Note__c" extensions="NoteExtensionCtrl" showHeader="false">
    <apex:form id="theForm">
        <table>
            <tr>
                <th>Note Name</th>
                <th>Note Body</th>
                <th rowspan="2"><apex:commandButton value="Save" action="{!Save}" rerender="theForm" /></th>
            </tr>
            <tr>
                
                <th><apex:inputField value="{!Note__c.Name}"/></th>
                <th><apex:inputField value="{!Note__c.Content__c}"/></th>
            </tr>
        </table>
    </apex:form>
</apex:page>


 
This was selected as the best answer