Tag Archives: Picklist

Set Default Value in Picklist in Lightning Component

Here in below example the default value in picklist is “Apple”.
Lightning Component:

<aura:component>
    <ui:inputSelect aura:Id="brandId" multiple="false" label="Select Brand">
        <ui:inputSelectOption label="Apple" text="Apple" value="true"/>
        <ui:inputSelectOption label="Nokia" text="Nokia"/>
        <ui:inputSelectOption label="Samsung" text="Samsung"/>
        <ui:inputSelectOption label="Google" text="Google"/>
        <ui:inputSelectOption label="Mi" text="Mi"/>
    </ui:inputSelect>
</aura:component>

Output:

Global Picklist Value Set in Salesforce

Global Picklist or Picklist Value Set is an Universal Picklist Values, which can be applied to any custom picklist fields that you create both on standard and custom objects.

Lets take an example, I have an object called “Student” and “Student Application” in these two object I have to create a picklist field called “Language”. So, here I’ve created a “Picklist Value Set” and used it on both object “Student Application”.

Click Setup || App Setup || Create || Picklist Value Set || Click ‘New’

Create “Language” Picklist Value Set:

Create “Language” Picklist from Picklist Value Set in “Student” and “Student Application” object:

Check “Language” Picklist Value Set, where it is used:

Note: You can have up to 500 picklist global value sets in an org. Each global value set, or restricted picklist, can contain a mix of 1,000 active and inactive values. Unrestricted picklists can have up to 1,000 active values. There’s no limit on the number of custom picklists that use global picklist value sets.

Picklist Value Set Considerations:

  • Validation rules are still defined at the field level, and are not part of the Picklist Value Set definition. This means, you can have the Business Unit picklist field on Account to respect a validation rule; and the same field on the Contact object not.
  • It is possible to create a mutli-select picklist field using a Picklist Value Set definition. However, in such cases the “replace” functionality available on regular picklist fields are not available.
  • Also, a picklist field created based on a Picklist Value Set cannot be used as a dependent picklist. It can still be used as a controlling picklist field. This is a known and documented limitation.

Populate Picklist from Custom Object to the Visualforce Page

Controller:

public with sharing class Sample
{
    public string selectedValue {get; set;}
    public List<SelectOption> industry {get; set;}
    
    public void getIndustry()
    {
        Schema.DescribeFieldResult industryDescription = Account.Industry.getDescribe();
        industry = new List<SelectOption>();
        
        for (Schema.Picklistentry pl : industryDescription.getPicklistValues())
        {
            industry.add(new SelectOption(pl.getValue(),pl.getLabel()));
        }
    }
    
    public void checkValue()
    {
        System.debug('Selected Industry Type - ' + selectedValue);
    }
}

Visualforce Page:

<apex:page controller="Sample" action="{!getIndustry}" sidebar="false" showHeader="false">
    <apex:form >
        <apex:pageblock>
            <apex:pageBlockSection columns="1" >
                <apex:outputLabel value="Industry Type" />
                <apex:selectList size="1" value="{!SelectedValue}" >
                    <apex:selectOptions value="{!industry}"/>
                    <apex:actionSupport event="onchange" action="{!checkValue}" />
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageblock>
    </apex:form>
</apex:page>

Output:

Get Default Picklist Field Value Using Apex

Sample Code:

String defaultLeadStatus = '';
//Get Lead object Status field description
Schema.DescribeFieldResult fieldDesc = Schema.Lead.Status.getDescribe();
//Loop on Status picklist field values
for (Schema.Picklistentry picklistEntry: fieldDesc.getPicklistValues()) {
    //Check if picklist value is default
    if (picklistEntry.isDefaultValue()) {
        defaultLeadStatus = picklistEntry.getValue();
        break;
    }
}