2

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.

1

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.

public class MyCustomObjectServices
{
    public static Schema.PicklistEntry myPicklistDefaultEntry
    {
        get
        {
            // Lazy load if you wish
            // You may prefer to return just the Label/Value
            for (Schema.PicklistEntry entry : myPickListEntries)
            {
                if (entry.isDefaultValue()) return entry;
            }
            return null;
        }
    }
    public static List<Schema.PicklistEntry> myPickListEntries
    {
        get
        {
            // Lazy load if you wish
            return MyCustomObject__c.MyPickList__c.
                getDescribe().getPicklistValues();
        }
    }
}

You could reference these in your controller with ease:

public MyCustomObjectExt(ApexPages.StandardController standardcontroller){
    MyCustomObject__c customObject = new MyCustomObject__c();
    customObject.MyPickList__c = MyCustomObjectServices.myPicklistDefaultEntry.getValue();
    this.controller = standardcontroller;
}
  • Thank you so much - a few questions. Is the sample code meant to be included in the controller extension? Where do I set the picklist value as readonly? Does my VF needs the code I added? My picklist default value is already set on the object and, in fact, <apex: output field value=> prints out 'New'. – paranza Feb 1 '15 at 1:35
  • It is read only because you are using an apex:outputField component. – Adrian Larson Feb 1 '15 at 1:36
  • Not sure where the gap is as your question is pretty vague. I tried to show you a programmatic approach to retrieving the default so if someone changes the configuration (default value), your code will keep up. – Adrian Larson Feb 1 '15 at 1:38
  • Thanks again, I am not fully clear where the changes would need to be applied in order to a) make 'new'as the default value b) lock down the field so that the user cannot edit it on the UI - it will be changed programmatically. – paranza Feb 1 '15 at 1:44
  • To reiterate, apex:outputField is read-only. See my updated answer for how you would incorporate in your controller. – Adrian Larson Feb 1 '15 at 1:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.