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
Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue 

setting the default value on a Visual Force page to match the default value of the field

I am trying to modify some VF code to use the field's default value as the default value on the page

Ctrl Code
 
public String mailingCountry {get; set;}

public PageReference saveAttendee(){
mailingCountry = attendee.DTCI_Mailing_Country__c;
}
 
VF Page Code
<detail>Mailing Country<font class='detailError'>*</font></detail>
<apex:inputField value="{!CnP_PaaS_EVT__Event_attendee_session__c.DTCI_Mailing_Country__c}" required="true" styleClass="detail" id="mailingCountryField" >                                        
<apex:param name="attendeeMailingCountryParam" value="{!CnP_PaaS_EVT__Event_attendee_session__c.DTCI_Mailing_Country__c}" assignTo="{!mailingCountry}"/>                                
 </apex:inputField>

The page displays all the appropriate values but does not set the default value to the field's default.  It's set to --None--

how do I set the default value for the page to the field's default value?

gratefully, A                 
Best Answer chosen by Amanda Byrne- Carolina Tiger Rescue
Terence_ChiuTerence_Chiu
You also seem to be accessing the field via the standard controller, if the record you are viewing as already been saved with the none value then that will be what is displayed.

In your controller class can you add a public variable of type CnP_PaaS_EVT__Event_attendee_session__c and initialize it from the constructor method of your controller class. For example:

public CnP_PaaS_EVT__Event_attendee_session__c ses {get; set;}

//replace ControllerClass with the name of your controller class
public ControllerClass(){
     ses = new CnP_PaaS_EVT__Event_attendee_session__c();
}


From your VF page replace this line:

<apex:inputField value="{!CnP_PaaS_EVT__Event_attendee_session__c.DTCI_Mailing_Country__c}" required="true" styleClass="detail" id="mailingCountryField" >

With this update line:

<apex:inputField value="{!ses.DTCI_Mailing_Country__c}" required="true" styleClass="detail" id="mailingCountryField" >

Let me know if the field displays the default value after the change.

All Answers

Terence_ChiuTerence_Chiu
Do you have any record types configured for the custom object ? If you do, the default value selection for the picklist for the record type could be superseding the default value selected from the picklist field.
Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue
No, no record types
Terence_ChiuTerence_Chiu
You also seem to be accessing the field via the standard controller, if the record you are viewing as already been saved with the none value then that will be what is displayed.

In your controller class can you add a public variable of type CnP_PaaS_EVT__Event_attendee_session__c and initialize it from the constructor method of your controller class. For example:

public CnP_PaaS_EVT__Event_attendee_session__c ses {get; set;}

//replace ControllerClass with the name of your controller class
public ControllerClass(){
     ses = new CnP_PaaS_EVT__Event_attendee_session__c();
}


From your VF page replace this line:

<apex:inputField value="{!CnP_PaaS_EVT__Event_attendee_session__c.DTCI_Mailing_Country__c}" required="true" styleClass="detail" id="mailingCountryField" >

With this update line:

<apex:inputField value="{!ses.DTCI_Mailing_Country__c}" required="true" styleClass="detail" id="mailingCountryField" >

Let me know if the field displays the default value after the change.
This was selected as the best answer
Amanda Byrne- Carolina Tiger RescueAmanda Byrne- Carolina Tiger Rescue
GOOD CATCH! It was because we set the default values for the field after the record had been created! It didn't occur to me that would be the problem. When I tried on different event check-in that created the attendee records after we had set the default value, it worked great! Thank you very much.