0

I'm using getPicklistValues to fetch the multiselect picklist values in LWC and using it for <lightning-checkbox-group>. But it is not showing anything. And if I use same code for <lightning-radio-group> output is showing correctly.

Below is my code

JS

@track eligiPicklistValues = [];    
@wire(getPicklistValues, {
            recordTypeId: "$objectInfo.data.defaultRecordTypeId",
            fieldApiName: ELIGIBILITY_REQUIREMENTS
        })
        EligibilityPicklistValues({ error, data }) {
            // reset values to handle eg data provisioned then error provisioned
            this.eligiPicklistValues = undefined;
            if (data) {
                let tempArray = [];
                data.values.forEach(picklistValue => {
                    let tempvalue = {};
                    tempvalue['label'] = picklistValue.label;
                    tempvalue['value'] = picklistValue.value;
                    console.log(tempvalue);
                    tempArray.push(tempvalue);
                    console.log(tempArray);
                })
                this.eligiPicklistValues = [...tempArray];
                console.log(this.eligiPicklistValues);
            } else if (error) {
                console.log('error');
                console.log(error);
            }
        } 

HTML

<!-- <lightning-radio-group name="eligility" label="Eligibility Requirements" options={eligiPicklistValues}
                        type="radio" value={dataObj.eligility} data-name="eligility" required={isRequired}
                        onchange={handleInputChange}>
                    </lightning-radio-group> -->

                    <lightning-checkbox-group name="eligility"
                              label="Eligibility Requirements"
                              options={eligiPicklistValues}
                              value={dataObj.eligility}
                              data-name="eligility"
                              onchange={handleInputChange}>
                    </lightning-checkbox-group> 

I have commented the radio group which is working.

1 Answer 1

0

The dataObj.eligility variable is null, resulting in the error.

Here is what I have tried and worked fiend: HTML:

<template>
        <p>Selected Values are: {selectedValues}</p> 
        <lightning-checkbox-group name="eligility" label="Eligibility Requirements" options={eligiPicklistValues}
                value={value} data-name="eligility" onchange={handleChange}>
        </lightning-checkbox-group>
</template>

JS:

import { LightningElement } from 'lwc';
import { getObjectInfo, getPicklistValues } from "lightning/uiObjectInfoApi";
import INDUSTRY_FIELD from "@salesforce/schema/Account.Industry";
import ACCOUNT_OBJECT from "@salesforce/schema/Account";

export default class CheckboxGroupBasic extends LightningElement {
        value = [];
        eligiPicklistValues = [];
        objectInfo;

        @wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
        objectInfo;

        @wire(getPicklistValues, {
                recordTypeId: '$objectInfo.data.defaultRecordTypeId',
                fieldApiName: INDUSTRY_FIELD
        }) 
        picklistValues({ error, data }) {
                if (data) {
                    console.log("data:: "+JSON.stringify(data.values));
                    data.values.forEach(picklistValue => {
                        this.eligiPicklistValues.push({
                            label: picklistValue.label,
                            value: picklistValue.value
                        });
                        
                    })
                    console.log("dataoption :: "+JSON.stringify(this.eligiPicklistValues));
                } else if (error) {
                    console.log('error');
                    console.log(error);
                }
        }


        get selectedValues() {
                return this.value.join(',');
        }

        handleChange(e) {
                this.value = e.detail.value;
        }
}

Output:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .