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
Shivendra Singh PawarShivendra Singh Pawar 

How to call a picklist into lightning component ?

Hi 

Greetings of the day!!!

I create a country picklist into contact object in Sales app and i want to use it into lightning component to select a country from the <select> in the form,

I am not getting, how I can call a picklist into a lightning component from.

<lightning:select aura:id="NewCon" label="Course" name="NewCon" type="String" value="{!v.NewCon.CourseCategory__c}" required="true"/>

Best Answer chosen by Shivendra Singh Pawar
Team NubesEliteTeam NubesElite
Hi Shivendra,
There are 2 ways to do it.
1. Do it using lightning:select where you describe set of options. https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/aura_compref_lightning_select.htm
<aura:component>
<lightning:select name="selectItem" label="Select an item" onchange="{!c.doSomething}">
    <option value="">choose one...</option>
    <option value="1">one</option>
    <option value="2">two</option>
</lightning:select>
</aura:component>
2. A better option will be to use <force:inputField> It will automatically determine the type and show checkbox, picklist or text field.
<aura:application extends="force:slds">
 <aura:attribute name="account" type="Account" 
           default="{ 'sobjectType': 'Account' }"/>
Account Name: <force:inputField value="{!v.account.Name}"/><br></br>
Ratings:  <force:inputField value="{!v.account.Risk_Rating__c}"/><!-- My picklist Field-->
</aura:application>


Thank You
www.nubeselite.com

Developement | Training | Consulting

Please Mark this assolution if your problem resolved.
 

All Answers

AmarpreetAmarpreet
Hi Shivendra,

Try this:
<aura:attribute name="con" type="Contact" default="{ 'sobjectType': 'Contact' }"/>
<force:inputField value="{!v.con.CourseCategory__c}"/>
Shivendra Singh PawarShivendra Singh Pawar
Hi Amarpreet...

Still, I am facing a same issue, picklist is not getting populated in form using lightning component and also getting an error

User-added image 

 
AmarpreetAmarpreet
The error you are getting is related to the String "--". You have a comment, which includes a "--" in it. Try to delete that commented part or remove "--".

Also, i did not see any attribute defined for a Contact. First create a Contact instance by using above attribute in your component.
 
<aura:attribute name="con" type="Contact" default="{ 'sobjectType': 'Contact' }"/>

After that, refer the picklist field in "force:inputField" as below:
 
<force:inputField value="{!v.con.CourseCategory__c}"/>

Hope, "CourseCategory__c" is the API name for picklist. If not, use tha correct API name. 

 
Shivendra Singh PawarShivendra Singh Pawar
Dear Amarpreet,

We already defined the attribute in this way :

<aura:attribute name="NewCon" 
                type="Contact" 
                default="{ 'sobjectType': 'Contact',
                           'FirstName':'',
                           'LastName': '',
                           'Email': '', 
                           'Phone': '',
                           'CourseCategory__c': ''}"/>

Kindly let me know how the picklist gets populated. I am defining my controls in this way all the controls working fine 

 <lightning:input aura:id="NewCon" label="First Name" name="NewCon" value="{!v.NewCon.FirstName}" required="true"/>
         

 Plz, let me know how can I use the picklist to select a country. 

Plz, suggest some answers asap.

Thanks & Regards
Shivendra

    
AmarpreetAmarpreet
Hi Shivendra,

I am not sure that why force:inputField is not working for you.

You can also try below steps:

1) Create a server-side method that returns list of all picklist values available.

2) Call this method when lightning component is initialized (i.e. init  method)

3) Iterate through list of picklist options using <aura:iteration> inside <lightning:select> and add each of them as <option>.
Team NubesEliteTeam NubesElite
Hi Shivendra,
There are 2 ways to do it.
1. Do it using lightning:select where you describe set of options. https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/aura_compref_lightning_select.htm
<aura:component>
<lightning:select name="selectItem" label="Select an item" onchange="{!c.doSomething}">
    <option value="">choose one...</option>
    <option value="1">one</option>
    <option value="2">two</option>
</lightning:select>
</aura:component>
2. A better option will be to use <force:inputField> It will automatically determine the type and show checkbox, picklist or text field.
<aura:application extends="force:slds">
 <aura:attribute name="account" type="Account" 
           default="{ 'sobjectType': 'Account' }"/>
Account Name: <force:inputField value="{!v.account.Name}"/><br></br>
Ratings:  <force:inputField value="{!v.account.Risk_Rating__c}"/><!-- My picklist Field-->
</aura:application>


Thank You
www.nubeselite.com

Developement | Training | Consulting

Please Mark this assolution if your problem resolved.
 
This was selected as the best answer