function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nithya S 1Nithya S 1 

Dynamic pick list

Hi, I'm trying to fetch picklist from a custom object but the value isn't being displayed. Get a blank dropdown. Please let me know what is missing..

Snippet Component :
 <aura:attribute name="Location" type="Loc__c" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
     <lightning:select name="Location" aura:id="Location">
                            <aura:iteration items="{!v.Location}" var="loc">
                            <option value="{!v.loc.Loc__c}">
                            </option>
                    </aura:iteration>

Controller :
({
    doInit: function(component,event,helper){
     var action = component.get("c.getLocation");
        action.setCallback(this, function(a){
            component.set("v.Location",a.getReturnValue());
                  
        });
        $A.enqueueAction(action)
    },

Class:
 public class LocSearchController {
            @AuraEnabled
                public static List<Location__c> getLocation(){
                    return [select Loc__c from Location__c];
                    
                 }
}
Ajay K DubediAjay K Dubedi

Hi Nithya,

Please try the code below.

<aura:attribute name="Location" type="String[]" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
     <lightning:select name="Location" aura:id="Location">
                            <aura:iteration items="{!v.Location}" var="loc">
                            <option value="{!v.loc.Loc__c}">
                            </option>
                    </aura:iteration>

Controller :
({
    doInit: function(component,event,helper){
     var action = component.get("c.getLocation");
        action.setCallback(this, function(a){
            component.set("v.Location",a.getReturnValue());
                  
        });
        $A.enqueueAction(action)
    },

public class LocSearchController
 {
   @AuraEnabled
   public static List<String> getLocation()
   {
	List<String> values = new List<String>();

    Schema.DescribeFieldResult fieldResult = Location__c.Loc__c.getDescribe();
	List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
	for(Schema.PicklistEntry p : ple)
	{
     values.add(p.getLabel());

    }
     return values;
   }
 }

Please mark it as Best if it helps.

Thanks
Ajay
Nithya S 1Nithya S 1
Hi Ajay,
Thanks for your response. But, the same result is the same, it just gives a blank dropdown.



 
Ajay K DubediAjay K Dubedi
Hey Nithya change <option value="{!v.loc.Loc__c}"> to <option value="{!v.loc}"> in line no. 5 or try new code by using wrapper class.
<aura:attribute name="Location" type="String[]" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<lightning:select name="Location" aura:id="Location">
<aura:iteration items="{!v.Location}" var="loc">
   <option value="{!v.loc.value}">{!loc.label}
   </option>
</aura:iteration>
Controller :
({
doInit: function(component,event,helper){
var action = component.get("c.getLocation");
action.setCallback(this, function(a){
component.set("v.Location",a.getReturnValue());
});
$A.enqueueAction(action)
},
public class LocSearchController
{
@AuraEnabled
public static List
<LocationWrapper>
getLocation()
{
List
<LocationWrapper>
getValues = new List
<LocationWrapper>
();
Schema.DescribeFieldResult fieldResult = Location__c.Loc__c.getDescribe();
List
<Schema.PicklistEntry>
ple = fieldResult.getPicklistValues();
for(Schema.PicklistEntry p : ple)
{
LocationWrapper obj = new LocationWrapper();
obj.label = p.getLabel();
obj.value = p.getValue();
getValues.add(obj);
}
return getValues;
}
private class LocationWrapper{
@AuraEnabled 
public String label{get;set;}
@AuraEnabled 
public String value{get;set;}
}
}

Thanks Ajay
Nithya S 1Nithya S 1

Hi Ajay,

The code change on line 5 didn't work; but the wrapper class did! Wondering why it didn't work without the wrapper. Any idea?
Thank you very much!

Ajay K DubediAjay K Dubedi
Hi Nithya, 
Above code is also working but there is no label for the option value that's why it's not visible. 
try this
<option value="{!v.loc}">{!v.loc}</option>
I used here wrapper because we are fetching two different values one is for Picklist value and other is for its name in salesforce org.
Thanks
Ajay Dubedi
Nithya S 1Nithya S 1
Hi Ajay,
No, I tried changing the option value and it again brought back the empty list. There are quite a number of picklists that I'd have to show on the component. Would I have to use the wrapper and  label option for all?

Thanks again!