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
BManojKumarBManojKumar 

Pre - Populate the field in visual force page

Hi All,

 

I would like to set the value for the account name field in visual force page when it is opening. I just want to pre-populate the value. I dont find the way to do it. Can anyone help me out ?

 

Thanks in Advance.

 

Manoj 

Best Answer chosen by Admin (Salesforce Developers) 
sinsiterrulezsinsiterrulez

Hi,

Here's how you can set the account value for oppty.

public class MyController
{
public opportunity oppty {get;set;}  
public MyController(ApexPage.standardController controller)
  {
    oppty = (Opportunity)controller.getrecord();
    oppty.Account = <VALUE>;
  }
}

 

 

All Answers

sinsiterrulezsinsiterrulez

Based on your logic you can prepopulate the account details in the constructor of the controller for your VF page

BManojKumarBManojKumar

Hi sinsiterrulez,

 

Can you give me any example for this. I just want to set the value for account name field.

 

Thanks

Manoj

Alok_NagarroAlok_Nagarro

Hi,

You can set the account name in the controller and using merge field populate field in page.

For example: let your controller like this...

 

public class MyController

{

  public MyController(ApexPage.standardController controller)

  {

    name=' any name' ;        

  }

 

 public String name{get; set;}
}

 

/*****************Page*******************/

 

<apex:page standardController="Account" extensions="MyController">

<apex:outputLabel>Name</apex:outputLabel>

<apex:outputField value="{!name}"/>

 

 

Thanks,

</apex:page>

 

lnryanlnryan
<apex:page standardController="Account">
<apex:outputField value="{!Account.Name}">
</apex:page>

 

If that's what you were looking for, you'll find the cookbook very helpful. It contains code samples for most introductory VF needs.

 

If that's not what you were looking for, maybe a use case would clarify?

lnryanlnryan

I don't think creating a controller for this is good advice here, since the standard controller provides the functionality to do this without a controller class. Controller classes require test coverage and increase development time / requirements.

sinsiterrulezsinsiterrulez

Hi,

Here's how you can set the account value for oppty.

public class MyController
{
public opportunity oppty {get;set;}  
public MyController(ApexPage.standardController controller)
  {
    oppty = (Opportunity)controller.getrecord();
    oppty.Account = <VALUE>;
  }
}

 

 

This was selected as the best answer