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

Prepopulating a lookup field in visualforce page

Hi All,


I have a look up field as Event Name in my registrant visualforce page.

When a user clicks on this registrant vf page via site ,  the lookup field Event Name should already have a predefined value in it .How is it possible ?


Can we change the url of vf page in such a way that the look up field will contain the value we want ?



I'm not sure I have this right, but it's my impression that you have a custom Registrant object with a custom lookup to another object, and you want to have a VF page that starts a new Registrant, with the lookup( Event Name) already filled in, waiting for the user to enter the rest of the information.


I think this would be done through a custom controller or controller extension. If it needs to be filled in dynamically, you could use a URL parameter (/apex/NewRegistrant?event=[the event Id]). Are you already using a custom controller or extension? Let us see your existing code if you have some.






I do have a custom controller .It inserts the record and directs it to a thanks page when register button is clicked.


I was trying by url parameters ..but could not do it .


Suppose the event id is =a06S0000002VZM3


so in my page i was trying


Can you please give me some example how to use url param to fill this look up field automatically.


My controller is



public  class  Registerbutton {

     private  final  ApexPages.StandardController  controller;
     public  Registerbutton (ApexPages.StandardController  stdController) {
         this.controller =  stdController;
     public  PageReference  Register()
       Registrant__c   reg = (Registrant__c)controller.getRecord();
       insert  reg;
       Pagereference  EventThankyou =  Page.EventThankyou  ;
       return  EventThankyou;

I added some lines to your code to keep track of the registrant as soon as the controller extension starts working. The new Registrant is initialized with the ID of your Event__c already, which should then fill in the field automatically. See if this makes sense. I didn't compile, so there may be typing errors.


public  class  Registerbutton {     
	private  final  ApexPages.StandardController ​ controller;
         Registrant__c reg;
	Event__c event;      
	public  Registerbutton (ApexPages.StandardCon​troller  stdController) {         
	  this.controller =  stdController;
	  if (ApexPages.CurrentPage().getparameters().get('event') != null) {
	  	this.event = [select id, Name from Event__c where id = :ApexPages.CurrentPage().getparameters().get('event')];
	  } else { 
	  	this.event = new Event__c();
	  reg = new Registrant__c( Event__c = );
	public Registrant__c getreg() { return reg; }
	public void setreg(Registrant__c r) { reg = r; }
	public  PageReference  Register()    {       
		//Registrant__c   reg = (Registrant__c)contro​ller.getRecord();              
		insert  reg;       
		Pagereference  EventThankyou =  Page.EventT​hankyou  ;       
		return  EventThankyou;