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
lakslaks 

Default value for inputfield in Visualforce

Hi,

 

How can we set a default value to an inputfield component in VF page.

I have an inputfield which is a lookup to an Object. I want a default value to be displayed on the field when the apge is loaded.

 

 

Regards,

Lakshmi.

Best Answer chosen by Admin (Salesforce Developers) 
lakslaks

I achieved it using the below logic:

 

In the constructor of controller:

m_objShipMethod = [Select Id, ShippingMethod__c from Shipping_Method__c where ShippingMethod__c = 'FedEx Ground' limit 1];
m_objOrderObj = [Select Shipping_Method__c from Order__c where Shipping_Method__c = :m_objShipMethod.Id limit 1];

 

In the VF page:
<apex:inputField value="{!m_objOrderObj.Shipping_Method__c}" styleClass="textbox"/>

 

 

Regards,

Lakshmi.

All Answers

LakshmanLakshman

you can do it as below:

 

<apex:inputField value="{!objCon.AccountId}" />

 

 

and in class you should have a variable like below

 

public Contact objCon {get;set;}
//In constructor
objCon = [Select AccountId from Contact where AccountId <> null limit 1];

 So you should set the Id of the object looked up to in the value parameter of inputField. Let me know if it works.

 

Regards,

Lakshman

 

 

 

lakslaks

HI Lakshman,

 

Thank you for the response.

 

I do get the lookup by doing the same.

 

But I was able to get that by associating the standardcontroller with the page and referring to the lookup field in inputfield tag also.

 

My code has: 

standardController="Order__c" extensions="OrderCreationController" in the apex:page tag

 

and the place where I am displaying the field has

<apex:inputField value="{!Order__c.Shipping_Method__c}" styleClass="textbox"/>

 

Shipping_Method__c is my object to which I want to have a lookup

 

That above code itself displays me the lookup in which all the possible shipping methods are listed.

My requirement here is that when the page loads I want the default shipping method among them to be displayed on the textfield.

 

 

Regards,

Lakshmi.

 

LakshmanLakshman

You will have to do an explicit query or create an instance instead like below

 

public Order__c objOrder{get;set;}

try{

objOrder = [Select Shipping_Method__c from Order__c where id=:validId ];

}catch(exception ex){

objOrder= new Order__c();

}

 

and then you use it in page

controller="OrderCreationController"  in the apex:page tag

<apex:inputField value="{!objOrder.Shipping_Method__c}" styleClass="textbox"/>

 

Regards,

Lakshman

 

lakslaks

I achieved it using the below logic:

 

In the constructor of controller:

m_objShipMethod = [Select Id, ShippingMethod__c from Shipping_Method__c where ShippingMethod__c = 'FedEx Ground' limit 1];
m_objOrderObj = [Select Shipping_Method__c from Order__c where Shipping_Method__c = :m_objShipMethod.Id limit 1];

 

In the VF page:
<apex:inputField value="{!m_objOrderObj.Shipping_Method__c}" styleClass="textbox"/>

 

 

Regards,

Lakshmi.

This was selected as the best answer