You need to sign in to do that
Don't have an account?

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.
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
you can do it as below:
and in class you should have a variable like below
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
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.
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
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.