Play Games

Search This Blog

Monday, May 17, 2021

fetch picklist field values in lightning salesforce - Salesforce Globe For You

 fetch picklist field values in lightning salesforce  - Salesforce Globe For You 

In this example,  the LeadSource picklist field values of Lead object are fetched and displayed in lwc component.

Example:

displayPicklistFieldValues.html

<template>

    <lightning-card  title="Display Lead Source Picklist Field Values">

        <p class="slds-p-horizontal_small">

            <lightning-combobox

            name="Lead Source"

            label="Lead Source"

            value={selectedPicklistValue}

            placeholder="Select Lead Source"

            options={leadSourcePicklistValues}

            onchange={handlePicklistChange} ></lightning-combobox>

        </p>

        <p>Selected Picklist Value is:{selectedPicklistValue}</p>

    </lightning-card>

</template>

displayPicklistFieldValues.js

import { LightningElement,wire,api,track} from 'lwc';

import { getPicklistValues } from 'lightning/uiObjectInfoApi';

import { getObjectInfo } from 'lightning/uiObjectInfoApi';

import LEAD_OBJECT from '@salesforce/schema/Lead';

import LEADSOURCE_FIELD from '@salesforce/schema/Lead.LeadSource';

export default class DisplayPicklistFieldValues extends LightningElement {

    @api leadSourcePicklistValues;

    @track selectedPicklistValue;

    @wire(getObjectInfo, { objectApiName:LEAD_OBJECT  }) leadObjectMetaData;

    @wire(getPicklistValues, { recordTypeId: '$leadObjectMetaData.data.defaultRecordTypeId', fieldApiName: LEADSOURCE_FIELD })

    fetchPiclistValue({data,error}){

        if(data) {

            this.leadSourcePicklistValues = data.values;

        } 

        if(error) {

            console.log(error);

        }

    }

    handlePicklistChange(evt) {

        this.selectedPicklistValue = evt.detail.value;

    }

}

displayPicklistFieldValues.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <apiVersion>51.0</apiVersion>

    <isExposed>true</isExposed>

    <targets>

        <target>lightning__RecordPage</target>

        <target>lightning__AppPage</target>

        <target>lightning__HomePage</target>

    </targets>

</LightningComponentBundle>

Output:



No comments:

Post a Comment