I am fairly new to Salesforce and recently came across an interesting problem for which I have been trying to find a solution. My app has a Custom Object called MyCustomObject__c
with a VF named MyCustomObject.page
that calls a the StandardController MyCustomObject
and its extension MyCustomObjectControllerExt
. The Custom Object has a picklist (MyPickList__c
with 3 values: New
, Active
and Expired
and whose default value is set to New
on the picklist object. What I would like to achieve is to make the picklist defaulted to 'New' and Read-Only each time a new record is created (via the New Standard Salesforce button). I have first made the change to the Page Layout on the object by selecting the Read-Only option, however the user is still able to set whichever value they want to pick when creating a new record instance. I have then included the following code snippet in the VF:
<apex:form>
<apex:pageBlock>
<apex:pageBlockSection columns="1" title="My Custom Object">
<apex:outputField value="{!MyCustomObject__c.MyPickList__c}" />
</apex:pageBlockSection>
</apex:pageBlock>
<apex:form></code>
I am not sure, however, how the controller extension needs further adapting to set the picklist to the default value. Currently, it includes the following:
public MyCustomObjectExt(ApexPages.StandardController standardcontroller){
MyCustomObject__c customObject = new MyCustomObject__c();
customObject.MyPickList__c = 'New';
this.controller = standardcontroller;
}
Am I leaning towards the right direction with this issue?
Thank you all for your help, much appreciated!
I.
Best Answer
As for the read-only aspect of your question, end users will not be able to modify the value on the page you have specified, as you are using an
apex:outputField
, which is read-only by default (it's the first thing mentioned in the documentation).Here is how you would retrieve the deault programatically. Check out the Schema.PicklistEntry documentation, specifically
isDefaultValue
.You could reference these in your controller with ease: