Tag Archives: Lightning

Dynamic Getting sObject Picklist Values on Lightning Component

In this example we will fetch Picklist field “Industry” values from “Account” object and set in ui:inputSelect on Lightning Component.

Apex Class:

public class GetPicklistValuesController {
    @AuraEnabled
    public static List <String> getPicklistValues(sObject obj, String fld) {
        
        List <String> pList = new list <String>();
        
        //Get the object type of the SObject.
        Schema.sObjectType objType = obj.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) {
            pList.add(a.getValue());
        }
        pList.sort();
        return pList;
    }
}

Component:

<!--TestComponent-->
<aura:component controller="GetPicklistValuesController" access="global" implements="force:appHostable">
    <aura:attribute name="objInfo" type="account" default="{sobjectType : 'Account'}" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>    
    <div class="slds-form-element">
        <label class="slds-form-element__label" for="select-01">Select Industry</label>
        <div class="slds-select_container">
            <ui:inputSelect  aura:id="accIndustry" class="slds-select"  change="{!c.onPicklistChange}"/>
        </div>
    </div>
</aura:component>

Component Helper:

({
    fetchPickListVal: function(component, fieldName, elementId) {
        var action = component.get("c.getPicklistValues");
        action.setParams({
            "obj": component.get("v.objInfo"),
            "fld": fieldName
        });
        var opts = [];
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state == "SUCCESS") {
                var allValues = response.getReturnValue();
                
                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);
    },
})

Component Controller:

({
    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"));
    },
})

Lightning App:

<!--TestApp-->
<aura:application extends="ltng:outApp" access="global">
    <c:TestComponent />
</aura:application>

Output:

JavaScript Array

An array is a collection of data values. we can store more than one values and retrieve values as per index. Also, we can store lists an array. Array indexes are zero-based, the first element in the array is 0, the second is 1, and so on.

Array Example:
var fruits = ["Apple", "Orange", "Banana", "Mango"];

Array Properties:
constructor: Returns the function that created the Array object’s prototype.
length: Sets or returns the number of elements in an array.
prototype: Allows you to add properties and methods to an Array object.

Array Methods:

concat(): Joins two or more arrays, and returns a copy of the joined arrays.

copyWithin(): Copies array elements within the array, to and from specified positions.

entries(): Returns a key/value pair Array Iteration Object.

every(): Checks if every element in an array pass a test.

fill(): Fill the elements in an array with a static value.

filter(): Creates a new array with every element in an array that pass a test.

find(): Returns the value of the first element in an array that pass a test.

findIndex(): Returns the index of the first element in an array that pass a test.

forEach(): Calls a function for each array element.

from(): Creates an array from an object.

includes(): Check if an array contains the specified element.

indexOf(): Search the array for an element and returns its position.

isArray(): Checks whether an object is an array.

join(): Joins all elements of an array into a string.

keys(): Returns a Array Iteration Object, containing the keys of the original array.

lastIndexOf(): Search the array for an element, starting at the end, and returns its position.

map(): Creates a new array with the result of calling a function for each array element.

pop(): Removes the last element of an array, and returns that element.

push(): Adds new elements to the end of an array, and returns the new length.

reduce(): Reduce the values of an array to a single value (going left-to-right).

reduceRight(): Reduce the values of an array to a single value (going right-to-left).

reverse(): Reverses the order of the elements in an array.

shift(): Removes the first element of an array, and returns that element.

slice(): Selects a part of an array, and returns the new array.

some(): Checks if any of the elements in an array pass a test.

sort(): Sorts the elements of an array.

splice(): Adds/Removes elements from an array.

toString(): Converts an array to a string, and returns the result.

unshift(): Adds new elements to the beginning of an array, and returns the new length.

valueOf(): Returns the primitive value of an array.