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
StaciStaci 

set default picklist value in VF Page

All I want to do is set the default value in a Status picklist to Implemented when the button is clicked.  It doesn't even need to be displayed in the pop-up.  Just when Save is clicked and the page renders back to the change record, the Status needs to be Implemented.  Is that possible without a controller?

 

Code

<apex:page standardController="Change__c" extensions="impStartEnd"> 
<apex:form id="frm"> 
<apex:detail subject="{!Change__c.Id}" relatedList="false" title="false"/> 
<apex:outputPanel id="tstpopup" rendered="{!IF(isDisplayPopUp ==true,true,false)}" > 
<apex:outputPanel styleClass="popupBackground" layout="block" /> 
<apex:outputPanel styleClass="custPopup" layout="block"> 
<apex:pageMessages >
</apex:pageMessages> 
<apex:pageBlock >
<apex:pageBlockSection > 
<apex:inputField label="Actual Implementation Start Date / Time" value="{!Change__c.Implementation_Start_Date_Time__c}"/>
<apex:inputField label="Actual Implementation End Date / Time" value="{!Change__c.Implementation_End_Date_Time__c}"/>

<apex:outputPanel > 
<apex:CommandButton action="{!save}" value="Save!"/> 
<apex:CommandButton action="{!cancel}" value="cancel"/> 
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
</apex:outputPanel>




</apex:form> 

<style type="text/css"> .errorMsg{ width:159px; } 
.custPopup{ background-color: white; border-width: 3px; 
border-style: solid; 
z-index: 9999; 
left: 25%; 
padding:10px;
position: absolute; 
width: 800px; 
//margin-left: -80px; top:100px; margin-left: -170px; 
//top:305px; 
border-radius: 5px; 
} 

.datePicker{z-index:10000}



.popupBackground{ background-color:black; opacity: 0.20; filter: alpha(opacity = 20); 
 position: absolute; width: 100%; height: 100%; top: 0; left: 0; 
 z-index: 997 } a.actionlink:hover{ text-decoration:underline; } 
 .customactionLink { color: #015BA7; font-weight: normal; text-decoration: none; } </style> 

 <script>
      function setFocusOnLoad() { }
</script>
</apex:page>

 

Avidev9Avidev9
Yes you can do this using Jquery/Javascript.
DaveHDaveH
Well you could use javascript but remember that manipulating built-in visualforce inputs via scripts can be risky because Salesforce could change the structure of how the component is rendered at any time. This could break your page. It's often better (and recommended) to post the change to a controller for a couple reasons:

1) You can bind the value of the picklist to the field of your object so whatever the value of the field is that is what the dropdown will be set to
2) You don't have to worry about Salesforce making a change to the HTML structure of components and breaking the page.

Just to summarize if you can bind the value to a field in the controller then it's often better to do that. If you need to use javascript just be aware that you might have to make changes down the road to accommodate changes made by Salesforce.
StaciStaci

Thanks Dave

 

How do I bind it in the controller?

 

This is all my controller does right now

public class impStartEnd { public Boolean isDisplayPopUp {get; set;} public impStartEnd(ApexPages.StandardController controller) { isDisplayPopUp = true; } }

 

DaveHDaveH

Here is an example right from the docs:

 

// Select list in VF page
<apex:selectList value="{!selectedCountry}" multiselect="false" size="1">
    <apex:selectOptions value="{!items}"/>
</apex:selectList>

/*************** Controller *******************/
public class sampleCon {
	public String selectedCountry {get; set;}
		
	public List<SelectOption> getItems() {
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('US','US'));
		options.add(new SelectOption('CANADA','Canada'));
		options.add(new SelectOption('MEXICO','Mexico'));
		return options;
	}
}

 So you can see there is a var in the controller named "selectedCountry" and it get's bound to the value attrib for the selectList. When the page is loaded it will pull the default value (if any) of selectedCountry and on form submit whatever the user has selected will get assigned to selectedCountry.

StaciStaci

Ok almost there!  I get the default now, but when I click Save, it doesn't update the record.

 

Page:

<apex:page standardController="Change__c" extensions="impStartEnd"> 
<apex:form id="frm"> 
<apex:detail subject="{!Change__c.Id}" relatedList="false" title="false"/> 
<apex:outputPanel id="tstpopup" rendered="{!IF(isDisplayPopUp ==true,true,false)}" > 
<apex:outputPanel styleClass="popupBackground" layout="block" /> 
<apex:outputPanel styleClass="custPopup" layout="block"> 
<apex:pageMessages >
</apex:pageMessages> 
<apex:pageBlock >
<apex:pageBlockSection > 
<apex:inputField label="Actual Implementation Start Date / Time" value="{!Change__c.Implementation_Start_Date_Time__c}" required="true"/>
<apex:inputField label="Actual Implementation End Date / Time" value="{!Change__c.Implementation_End_Date_Time__c}" required="true"/>
<apex:selectList label="Status" value="{!selectedStatus}"  multiselect="false" size="1">
   <apex:selectOptions value="{!Items}"/>
</apex:selectList>  
 

<apex:outputPanel > 
<apex:CommandButton action="{!save}" value="Save!"/> 
<apex:CommandButton action="{!cancel}" value="cancel"/> 
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
</apex:outputPanel>





</apex:form> 



<style type="text/css"> .errorMsg{ width:159px; } 
.custPopup{ background-color: white; border-width: 3px; 
border-style: solid; 
z-index: 9999; 
left: 25%; 
padding:10px;
position: absolute; 
width: 800px; 
//margin-left: -80px; top:100px; margin-left: -170px; 
//top:305px; 
border-radius: 5px; 
} 

.datePicker{z-index:10000}



.popupBackground{ background-color:black; opacity: 0.20; filter: alpha(opacity = 20); 
 position: absolute; width: 100%; height: 100%; top: 0; left: 0; 
 z-index: 997 } a.actionlink:hover{ text-decoration:underline; } 
 .customactionLink { color: #015BA7; font-weight: normal; text-decoration: none; } </style> 

 <script>
      function setFocusOnLoad() { }
</script>
</apex:page>

 Class:

public class impStartEnd 
{ 
public Boolean isDisplayPopUp {get; set;} 
public impStartEnd(ApexPages.StandardController controller) 
{ 
isDisplayPopUp = true; 
} 




    public String selectedStatus {get; set;}
        
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('IMPLEMENTED','Implemented'));
        return options;
    }

}