
How to Fetch Picklist value from sObject and Set in ui:inputSelect
Prerequisites : basic understanding of Lightning Component and apex programming
In this Sample code we will fetch Picklist field (‘Industry’) values from Account object and set in ui:inputSelect on lightning Component.

sample code for fetch Picklist values from sObject
apex class [ fetchPicklistOptsController.apxc ]
public class fetchPicklistOptsController { @AuraEnabled public static List < String > getselectOptions(sObject objObject, string fld) { system.debug('objObject --->' + objObject); system.debug('fld --->' + fld); List < String > allOpts = new list < String > (); // Get the object type of the SObject. Schema.sObjectType objType = objObject.getSObjectType(); // Describe the SObject using its object type. Schema.DescribeSObjectResult objDescribe = objType.getDescribe(); // Get a map of fields for the SObject map < String, Schema.SObjectField > fieldMap = objDescribe.fields.getMap(); // Get the list of picklist values for this field. list < Schema.PicklistEntry > values = fieldMap.get(fld).getDescribe().getPickListValues(); // Add these values to the selectoption list. for (Schema.PicklistEntry a: values) { allOpts.add(a.getValue()); } system.debug('allOpts ---->' + allOpts); allOpts.sort(); return allOpts; } }
Component [sample.cmp]
<aura:component controller="fetchPicklistOptsController"> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <aura:attribute name="objInfo" type="account" default="{sobjectType : 'Account'}" /> <div class="slds-form-element"> <label class="slds-form-element__label" for="select-01">Select Label</label> <div class="slds-select_container"> <ui:inputSelect aura:id="accIndustry" class="slds-select" change="{!c.onPicklistChange}"/> </div> </div> </aura:component>
Controller [ sampleController.js]
({ doInit: function(component, event, helper) { helper.fetchPickListVal(component, 'Industry', 'accIndustry'); }, onPicklistChange: function(component, event, helper) { // get the value of select option alert(event.getSource().get("v.value")); }, })
Helper [sampleHelper.js]
({ fetchPickListVal: function(component, fieldName, elementId) { var action = component.get("c.getselectOptions"); action.setParams({ "objObject": component.get("v.objInfo"), "fld": fieldName }); var opts = []; action.setCallback(this, function(response) { if (response.getState() == "SUCCESS") { var allValues = response.getReturnValue(); if (allValues != undefined && allValues.length > 0) { opts.push({ class: "optionClass", label: "--- None ---", value: "" }); } for (var i = 0; i < allValues.length; i++) { opts.push({ class: "optionClass", label: allValues[i], value: allValues[i] }); } component.find(elementId).set("v.options", opts); } }); $A.enqueueAction(action); }, })
TestApp.app
<aura:application extends="force:slds"> <c:sample/> <!-- here c: is org. namespace prefix--> </aura:application>
Like our facebook page for new post updates.
43 comments
Hiii Piyush soni,
Thanks for your great post. I am looking same feature for last 2 days and finally i got this post.
I need a help if you can please
This is my task
To get the pick list value
1. Get the record type name of the related deal and then based on deal record type name load the investor__c record id.
2. Based on record type id found in first step create an object of Investor__c object
I need “Status” options list . How to i do this (I have record type eg: ‘Financing’)
HI Tariq
thanks for your feedback
and i am not getting your requirement properly so you can post your query on http://sfdcmonkey.com/community/ , so i can better understand it.
Hiii Piyush soni,
Thanks for your great post. I am looking same feature with slight difference , i hope u ‘ll give best sol.
i have 3 pick lists in my page , i have to fetch from custom object with where condition like
1st pick list loads with employee names where from country UK, 2nd pick list loads with employee name where from country USA, like 3rd one also same with different country.
Thanks In advance
Hi Dandamudi,
sorry for late reply, here i post sample code for it Please refer below link
http://sfdcmonkey.com/community/main-forum/fetch-the-values-and-bind-to-3-ui-input-selects-from-single-sobject/#post-63
thanks
Hi Piyush
http://sfdcmonkey.com/community/main-forum/fetch-the-values-and-bind-to-3-ui-input-selects-from-single-sobject/#post-63
I do have same requiremnt but I am not able to open above link.
sorry this link is expired.
Thanks Piyush. Can you give me suggestion how to do it.
Hi Piyush,
How to make this picklist as required field?
on click on submit button check the value of picklist field using aura:id for example :
if(component.find("accIndustry").get("v.value") == ''){
alert('picklist field is required');
}
Thank you for this awesome post ! I tried several solutions but yours is the best (and above all the best explaned one) . Thanks for sharing your knowledge. I’m a person who need to see concrete examples to understand and you saved me waaaaay more than several times 🙂
woow .. thanks @catherine for your valuable feedback 🙂
Hi,
I tried the above code in my lightning component to fetch and update picklist values and it worked fine. But when I give it inside lightning tabset ‘<lightning:tabset >
<lightning:tab >’, I am getting this error :
‘Uncaught Error in $A.getCallback() [Cannot read property ‘set’ of undefined]
Callback failed: apex://NAT_RForceCases_VFC/ACTION$getselectOptions’. How to fix this?
Thanks
Aruna
Hi Aruna,
can you share your code here :
https://developer.salesforce.com/forums
and share question link here so i can look into this
Thanks
Hi Piyush,
Please check this link for my code.
Thanks
Aruna
Hi,
please refer this link.
Thanks
Aruna
HI! Everywhere I look online your code is the most popular. I am struggling a bit to understand what to change. I want to make it so that I can look up a picklist for a custom object for Client Payments (api would be Payment__c) and then I want to be able to choose the picklists in that custom object which would be things like Area of Concern, Organization… etc.
What about custom object picklists. Thanks
HI Stevie,
for custom object you just need to change in javaScript controller:
helper.fetchPickListVal(component, ‘CustomFildApiName__c’, ‘accIndustry’);
and in your lightning component attribute
aura:attribute name=”objInfo” type=”customObj__c” default=”{sobjectType : ‘customObj__c’}” />
if you have any issue with it you can post your query in description here :
https://developer.salesforce.com/forums
and share your question link here so we i can look into it
Thanks!!! Awesome.
Thanks for your feedback @Gus
Thanks for blog its helpful 🙂
Thanks for your feedback @Veena
Hi Piyush
Can u plz exlain with another picklist i mean two picklist fields
can you elaborate your issue, and post your query in details here :
https://developer.salesforce.com/forums
and share URL here of your post, so i can look into it.
Hi Piyush,
Do you have any lecture of your (videos) to understand about lightning.I am new to salesforce .
Hi, This post was very useful. Could you please let me know how to use lightning:select for the same example instead of ui:input
Thank you for the helpful post. Could you please let me know how to use lightning:select instead of inputselect for the same example.
implementing the same in can be done this way.
has attributes. we can use “value” attribute to dynamically populate values from the picklist of your choice. you can also use to place them on the page as explained in above piece of code. Thanks!
Helpful post! Thanks for sharing.
Thank You! It’s Great!!
implementing the same in <lightning:select> can be done this way.
has attributes. we can use “value” attribute to dynamically populate values from the picklist of your choice. you can also use <aura:iteration> to place them on the page as explained in above piece of code. Thanks!
Great post with solid example
While this solution is working on desktop, on mobile, we have a small issue. In mobile, after the inout is selected, it doesn’t show the selected value until the cursor or current selection of field is moved to another field
Its a great code,
Can you please let me know how I can get values form the Existing class, I am using force:recordData and I have few fields which has picklist already that i want to show when new record is getting created.
can this be achieved out of the box without writing custom code?
Hi,
Please, anyone, help me for whenever select the picklist value show the help text message.
How we can achieve this requirement.
Example:
Picklist values : A1,A2,A3,
I need to the show help text selected value.
Example : A1-Text1(Help text)
A2-Text2(Help text)
A3-Text3(Help text)
Please provide me a solution.
Thanks Advance,
kiran
What would it take to update this code to support filtering the values returned based on Record Type?
Hi,
I had the same requirement before and I used Salesforce User Interface API. Works for dependent picklists too.
Resources:
https://developer.salesforce.com/docs/atlas.en-us.218.0.uiapi.meta/uiapi/ui_api_get_started.htm
https://developer.salesforce.com/docs/atlas.en-us.218.0.uiapi.meta/uiapi/ui_api_features_records_dependent_picklist.htm
Thanks,
K
Hi piyush,
I have tried this code to one picklistfield is fine but in the same component i tried to get one more picklistfield i am getting error.How to solve this problem
Hi,
If there are 2 or more record type of account and each record type has a different set of picklist values, how do you get the values of the picklist for a specific record type with this solution?
I used the SOAP API for this but I want to know if there is a way to do it without doing callouts.
Thanks!
Please help me as well, I need to populate the combobox with Objects dynamically and populate the next combobox with Object’s fields.