By using lightning component, you can Retrieve List of Picklist values from any custom and Standard object.Lightning Component can be Reused in Different components by passing object Name and Field Name.
Create Apex Class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PickListController { | |
@AuraEnabled | |
public static List<String> getPickListValuesIntoList(String objectType, String selectedField){ | |
List<String> pickListValuesList = new List<String>(); | |
Schema.SObjectType convertToObj = Schema.getGlobalDescribe().get(objectType); | |
Schema.DescribeSObjectResult res = convertToObj.getDescribe(); | |
Schema.DescribeFieldResult fieldResult = res.fields.getMap().get(selectedField).getDescribe(); | |
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues(); | |
for( Schema.PicklistEntry pickListVal : ple){ | |
pickListValuesList.add(pickListVal.getLabel()); | |
} | |
return pickListValuesList; | |
} | |
} |
Lightning PicklistComponent:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<aura:component controller="PickListController" access="global" > | |
<aura:attribute name="sObjectName" type="String" /> | |
<aura:attribute name="fieldName" type="String" /> | |
<aura:attribute name="picklistValues" type="Object" /> | |
<aura:handler name="init" value="{!this}" action="{!c.doInit}" /> | |
</aura:component> |
LightningPicklistController.js:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
doInit : function(component) { | |
var action = component.get("c.getPickListValuesIntoList"); | |
action.setParams({ | |
objectType: component.get("v.sObjectName"), | |
selectedField: component.get("v.fieldName") | |
}); | |
action.setCallback(this, function(response) { | |
var list = response.getReturnValue(); | |
component.set("v.picklistValues", list); | |
}) | |
$A.enqueueAction(action); | |
} | |
}) |
You can use this component in different Lightning components to get Picklist field and its value.Below code should in after at the starting of component tag.
<aura:attribute name=”picklistValues” type=”Object” />
<c:PicklistValues sObjectName=”Opportunity” fieldName=”StageName” picklistValues=”{!v.picklistValues}” />
Modify the sobjectName and fieldName to Retrieve picklist field and its values from objects.
Paste this below Line of code in Components and based on the sobjectName and fieldName it will Retrieve picklist field.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<lightning:layoutItem flexibility="auto" padding="around-small"> | |
<div class="custom-box"> | |
<lightning:select value="{!v.Opportunity.StageName}"> | |
<option value="">Choose one…</option> | |
<aura:iteration items="{!v.picklistValues}" var="item"> | |
<option value="{!item.value}"> | |
{!item} | |
</option> | |
</aura:iteration> | |
</lightning:select> | |
</div> | |
</lightning:layoutItem> |
Reference:
https://trailhead.salesforce.com/projects/workshop-override-standard-action/steps/override_2