Hey guys, today in this post we are going to learn about How to Get Picklist Values Dynamically and display Picklist values & Radio Buttons value in Lightning Component Salesforce.
Files we used in this post example
dropdownCmpApp.app | Lightning Application | It is used for call the component to preview on browser |
dropdownCmp.cmp | Lightning Component | It is used for create a picklist and radio button on lightning component. |
dropdownCmpController.js | JavaScript Controller File | It is hold Javascript doInit and picklist change functionality to display picklist and radio values in lightning component. |
dropdownCmpHelper.js | JavaScript Controller Helper File | It is hold Javascript Helper Function |
dropdownCmpCtrl.apxc | Apex Class Controller | It is used to get picklist value from sObject throgh apex method |
scoreCard__c | Custom Object | Custom Object to use get picklist values dynamic |
targetVSActuals__c | Custom Fields | Custom Object Picklist Values
Sales Target |
Live Demo
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
Create Lightning Application
Step 1:- Create Lightning Application : dropdownCmpApp.app
From Developer Console >> File >> New >> Lightning Application
dropdownCmpApp.app [Lightning Component Application]
<aura:application extends="force:slds">
<c:dropdownCmp/>
</aura:application>
Create Lightning Component
Step 2:- Create Lightning Component : dropdownCmp.cmp
From Developer Console >> File >> New >> Lightning Component
dropdownCmp.cmp [Lightning Component]
<aura:component controller="dropdownCmpCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="statusListItems" type="string[]"/>
<aura:attribute name="targetVal" type="string"/>
<aura:attribute name="isMarried" type="string"/>
<div class="slds slds-p-around--medium">
<div class="slds-grid slds-wrap">
<div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">
<div class="slds-form-element">
<label class="slds-form-element__label">Target vs Actuals</label>
<div class="slds-form-element__controller">
<ui:inputSelect class="slds-select" aura:id="targetId" change="{!c.changeTarget}" value="{!v.targetVal}">
<ui:inputSelectOption text="--None--"/>
<aura:iteration items="{!v.statusListItems}" var="statusVar">
<ui:inputSelectOption text="{!statusVar}"/>
</aura:iteration>
</ui:inputSelect>
</div>
</div>
</div>
<div class="slds-col slds-size_6-of-12 slds-p-horizontal--small slds-m-bottom--small">
<div class="slds-grid slds-wrap">
<div class="slds-col slds-size_6-of-12">
<div class="slds-form-element slds-is-required">
<label class="slds-form-element__label">Is Married?</label>
<div class="slds-form-element__controller">
<div class="slds-grid slds-wrap slds-m-top--x-small">
<label class="slds-radio">
<input type="radio" id="yesOption" class="slds-m-right--medium" name="isMarried" value="Yes" onclick="{!c.setMarriedVal}"/>
<span class="slds-radio--faux"></span>
<span class="slds-form-element__label"> Yes</span>
</label>
<label class="slds-radio">
<input type="radio" id="noOption" name="isMarried" value="No" onclick="{!c.setMarriedVal}"/>
<span class="slds-radio--faux"></span>
<span class="slds-form-element__label"> No</span>
</label>
</div>
</div>
</div>
</div>
<div class="slds-col slds-size_6-of-12">
<aura:if isTrue="{!v.isMarried == 'Yes'}">
<div class="slds-form-element slds-is-required slds-m-top--small">
<label class="slds-form-element__label">Spouse Name</label>
<ui:inputText class="slds-input" aura:id="spouseName" />
</div>
</aura:if>
</div>
</div>
</div>
</div>
<aura:if isTrue="{!or(v.targetVal == 'Sales Target', v.targetVal == 'Score Value')}">
<div class="slds-grid slds-wrap">
<div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">
<div class="slds-form-element">
<label class="slds-form-element__label">Name</label>
<div class="slds-form-element__controller">
<ui:inputText class="slds-input" value="" aura:id="userName"/>
</div>
</div>
</div>
<div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">
<div class="slds-form-element">
<label class="slds-form-element__label">Designation</label>
<div class="slds-form-element__controller">
<ui:inputText class="slds-input" value="" aura:id="designation"/>
</div>
</div>
</div>
</div>
</aura:if>
<aura:if isTrue="{!or(v.targetVal == 'Achievement %', v.targetVal == 'Collections Target')}">
<div class="slds-grid slds-wrap">
<div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">
<div class="slds-form-element">
<label class="slds-form-element__label">Employee Code</label>
<div class="slds-form-element__controller">
<ui:inputText class="slds-input" value="" aura:id="employeeCode"/>
</div>
</div>
</div>
<div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">
<div class="slds-form-element">
<label class="slds-form-element__label">Annual Salary</label>
<div class="slds-form-element__controller">
<ui:inputText class="slds-input" value="" aura:id="annual"/>
</div>
</div>
</div>
</div>
</aura:if>
<br/><br/>
<!--Start RelatedTopics Section-->
<div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
<br/><br/>
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like →</span> </strong></p>
<div style="display:block; overflow:hidden;">
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
<li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
<li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
<li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
</ul>
</div>
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
<li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
<li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
<li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
</ul>
</div>
<div style="clear:both;"></div>
<br/>
<div class="youtubeIcon">
<a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
</div>
</div>
</div>
<!--End RelatedTopics Section-->
</div>
</aura:component>
Create Component JavaScript Controller
Step 3:- Create Lightning Component : dropdownCmpController.js
From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller
dropdownCmpController.js [JavaScript Controller]
({
doInit : function(component, event, helper) {
helper.statusListItemsHelper(component);
},
changeTarget:function(component, event, helper){
var targetVal = component.find('targetId').get('v.value');
//alert('targetVal ' + targetVal);
component.set('v.targetVal}',targetVal);
},
setMarriedVal: function(component,event) {
var val = event.currentTarget.value;
//alert(val);
component.set("v.isMarried",val);
},
})
Create Component JavaScript Helper
Step 4:- Create Lightning Component : dropdownCmpHelper.js
From Developer Console >> File >> New >> Lightning Component >> JavaScript Helper
dropdownCmpHelper.js [JavaScript Helper]
({
statusListItemsHelper : function(component, event, helper) {
var action = component.get('c.pickList');
action.setParams({"obj":"scoreCard__c", "str":"targetVSActuals__c"});
action.setCallback(this, function(response){
var state = response.getState();
if(state == "SUCCESS"){
var result = response.getReturnValue();
component.set('v.statusListItems',result);
}
});
$A.enqueueAction(action);
},
})
Create Apex Class Controller
Step 5:- Create Apex Class : dropdownCmpCtrl.apxc
From Developer Console >> File >> New >> Apex Class
dropdownCmpCtrl.apxc [Apex Class Controller]
public class dropdownCmpCtrl {
@AuraEnabled
public static List<String> pickList(String obj, String str) {
List<String> regList = NEW List<String>();
Schema.DescribeFieldResult plistvalues = Schema.getGlobalDescribe().get(obj).getDescribe().fields.getMap().get(str).getDescribe();
FOR(PicklistEntry ent:plistvalues.getpicklistvalues()) {
regList.add(ent.getLabel());
}
RETURN regList;
}
}
Further post that would you like to learn in Salesforce
How do I use dynamic forms in Salesforce?
When can I get Salesforce Dynamic Forms? Simply go to the Lightning record page of a custom object and select the “Highlights Panel” or the “Record Detail” component, then you'll see the option to “Upgrade Now” and can choose between migrating an existing page or starting from scratch.
What is restricted picklist in Salesforce?
Restricted picklists keep users from adding new values (either through the API or other apps). This restriction is useful for keeping your data consistent.
What is difference between dynamic and page layout?
An instant upgrade from page layouts: Place fields and sections wherever you want. Dynamic layouts: Use visibility rules to show and hide fields and sections. Simpler layout management: Manage the fields and sections on your pages in the Lightning App Builder without touching the page layout editor.
Related Topics | You May Also Like
Our Free Courses →
👉 Get Free Course →
📌 Salesforce Administrators 📌 Salesforce Lightning Flow Builder 📌 Salesforce Record Trigger Flow Builder |
👉 Get Free Course →
📌 Aura Lightning Framework 📌 Lightning Web Component (LWC) 📌 Rest APIs Integration |