You need to sign in to do that
Don't have an account?

How to fetch picklist values in lightning component using schema.
I am trying to pull picklist values in to my component using schema and its methods. I am getting all the values through server controller, but in my component labels are not populating but it is showing picklist structure.
Lightning Component:
<aura:component controller="PickListController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="aname" type="Account" default="{'sObjectType':'Account'}"/>
<aura:attribute name="picvalue" type="List"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<lightning:input label="Enter Your Name: " value="{!v.aname.Name}"/>
<lightning:input label="Phone Number: " value="{!v.aname.Phone}"/>
<lightning:select value="{!v.aname.Rating}" label="Rating">
<option value="choose">Choose one...</option>
<aura:iteration items="{!v.picvalue}" var="s">
<option value="{!s.value}"/>
</aura:iteration>
</lightning:select>
<lightning:button label="Submit" onclick="{!c.go}"/>
</aura:component>
Js:
({
doInit : function(component) {
var pickvar = component.get("c.getPickListValuesIntoList");
pickvar.setCallback(this, function(response) {
var state = response.getState();
if(state === 'SUCCESS'){
var list = response.getReturnValue();
component.set("v.picvalue", list);
}
else if(state === 'ERROR'){
//var list = response.getReturnValue();
//component.set("v.picvalue", list);
alert('ERROR OCCURED.');
}
})
$A.enqueueAction(pickvar);
},
go : function(component){
var cvar = component.get("v.aname");
var action = component.get("c.insertValues");
action.setParams({acc: cvar});
action.setCallback(this, function(response) {
var state = response.getState();
if(state === 'SUCCESS'){
var list1 = response.getReturnValue();
//component.set("v.picklistValues", list);
alert('Record Created Successfully '+list1);
}
else if(state === 'INCOMPLETE'){
alert('Something is missing');
}
else if(state === 'ERROR'){
alert('Insertion Failed');
}
})
$A.enqueueAction(action);
}
})
Server Controller:
public class PickListController {
@AuraEnabled
public static List<String> getPickListValuesIntoList(){
List<String> pickListValuesList = new List<String>();
Schema.DescribeFieldResult fieldResult = Account.Rating.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for( Schema.PicklistEntry pickListVal : ple){
pickListValuesList.add(pickListVal.getLabel());
System.debug('Values in Rating are: '+pickListValuesList);
}
return pickListValuesList;
}
@AuraEnabled
public static Id insertValues(Account acc){
insert acc;
return acc.id;
}
}

Lightning Component:
<aura:component controller="PickListController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="aname" type="Account" default="{'sObjectType':'Account'}"/>
<aura:attribute name="picvalue" type="List"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<lightning:input label="Enter Your Name: " value="{!v.aname.Name}"/>
<lightning:input label="Phone Number: " value="{!v.aname.Phone}"/>
<lightning:select value="{!v.aname.Rating}" label="Rating">
<option value="choose">Choose one...</option>
<aura:iteration items="{!v.picvalue}" var="s">
<option value="{!s.value}"/>
</aura:iteration>
</lightning:select>
<lightning:button label="Submit" onclick="{!c.go}"/>
</aura:component>
Js:
({
doInit : function(component) {
var pickvar = component.get("c.getPickListValuesIntoList");
pickvar.setCallback(this, function(response) {
var state = response.getState();
if(state === 'SUCCESS'){
var list = response.getReturnValue();
component.set("v.picvalue", list);
}
else if(state === 'ERROR'){
//var list = response.getReturnValue();
//component.set("v.picvalue", list);
alert('ERROR OCCURED.');
}
})
$A.enqueueAction(pickvar);
},
go : function(component){
var cvar = component.get("v.aname");
var action = component.get("c.insertValues");
action.setParams({acc: cvar});
action.setCallback(this, function(response) {
var state = response.getState();
if(state === 'SUCCESS'){
var list1 = response.getReturnValue();
//component.set("v.picklistValues", list);
alert('Record Created Successfully '+list1);
}
else if(state === 'INCOMPLETE'){
alert('Something is missing');
}
else if(state === 'ERROR'){
alert('Insertion Failed');
}
})
$A.enqueueAction(action);
}
})
Server Controller:
public class PickListController {
@AuraEnabled
public static List<String> getPickListValuesIntoList(){
List<String> pickListValuesList = new List<String>();
Schema.DescribeFieldResult fieldResult = Account.Rating.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for( Schema.PicklistEntry pickListVal : ple){
pickListValuesList.add(pickListVal.getLabel());
System.debug('Values in Rating are: '+pickListValuesList);
}
return pickListValuesList;
}
@AuraEnabled
public static Id insertValues(Account acc){
insert acc;
return acc.id;
}
}
Change your Lightning:select as below then it works:
<lightning:select value="{!v.aname.Rating}" label="Rating">
<option value="choose">Choose one...</option>
<aura:iteration items="{!v.picvalue}" var="s">
<option value="{!s}">{!s}</option>
</aura:iteration>
</lightning:select>
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Maharajan.C
All Answers
Change your Lightning:select as below then it works:
<lightning:select value="{!v.aname.Rating}" label="Rating">
<option value="choose">Choose one...</option>
<aura:iteration items="{!v.picvalue}" var="s">
<option value="{!s}">{!s}</option>
</aura:iteration>
</lightning:select>
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Maharajan.C