1

I am trying to retrieve selected values of multivalued picklist from the Salesforce record and mapping them into a dropdown list of a LWC component. In order to retrieve, I have used wire adapters as below.

So far I can retrieve the prepopulated values on the salesforce records as semicolon separated values. eg:45;78;80. What I need is seperating those values and show them as dropdown values in my lwc component

 import {
    LightningElement,
    api,
    wire,
    track
} from 'lwc';
import {
    getObjectInfo,
    getPicklistValues
} from 'lightning/uiObjectInfoApi';
import {
    getRecord,
    getFieldValue,
    getFieldDisplayValue
} from 'lightning/uiRecordApi';
import COURSE_ORDER1_OBJECT from '@salesforce/schema/Course_Order__c';
import CREDIT_VALUES from '@salesforce/schema/Course_Order__c.Organization__r.Credit_Value__c';

const fields = [AC_SUBJECT_AREA, CREDIT_VALUES];


export default class lwcAssociations extends LightningElement {
    @api title = "LWC Association"
   
    @api credit_values = "Credit Value";
    @api button = "FINISH";

    @api recordId;
    @track value;
    @track credit_val_list=[];

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

    @wire(getPicklistValues, { recordTypeId: '$objectInfo.data.defaultRecordTypeId', fieldApiName: CREDIT_VALUES})
    creditValues;


    get creditValues(){
        return getFieldValue(this.record.data,CREDIT_VALUES);
       
    }

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

    connectedCallback(){
        var credit_values = this.creditValues;
     
        this.credit_val_list = credit_values.split(',').map((fld) => fld.trim());
    }


}

Can someone help me splitting these values seperated by semiclons and map them into the picklist field in the template?

   <template>

    <div style="margin:20px; margin-bottom:150px; ">
        <lightning-card>

            <div>
                <header class="slds-modal__header">
                    <h6 id="modal-heading-06" class="slds-text-heading_medium slds-hyphenate"><b>{title}</b></h6>
                </header>
                <br />
                <!-- 1st row -->


                    <div class="oneline">
                        <template if:true={credit_val_list}>

                            <lightning-combobox label={creditValues} name="creditValue" value={value} options={credit_val_list}
                                onchange={handleChange}></lightning-combobox>
                        </template>

                    </div>
                    
           </div>

        </lightning-card>
    </div>

</template>
2

lightning-combobox does not support multiselect. You'll have to use lightning-dual-listbox

Also, the correct way to pass options to a lightning-combobox or lightning-dual-listbox is to pass list of objects with value and label property.

Markup -

<lightning-dual-listbox options={options}></lightning-dual-listbox>

JS -

 export default class LwcAssociations extends LightningElement {

    @track
    options = [
        { label: 'English', value: 'en' },
        { label: 'German', value: 'de' },
        { label: 'Spanish', value: 'es' }
    ]

 }
10
  • I am retrieving multipicklist values from salesforce record using wire adapters and just wanna display them in a dropdown. Actually want to map the retrieved data into dropdown – Ruklani Attanyake Jan 18 at 12:02
  • What will be the return value of wire adapter? A comma separated values? – Jay Rathod Jan 18 at 12:06
  • semi colon seperated values – Ruklani Attanyake Jan 18 at 12:08
  • Ok, help me understand. So, is it something like, you need to display a multiselect picklist field with values prepopulated based on a record's values? – Jay Rathod Jan 18 at 12:10
  • You are correct. Those prepopulated values can be retrieved by wire adapter with the help of get credit2Values() as values seperated by semicolons. Eg: 45;78;65. These returns values are just prepopulated on the salesforce record. So what I need is showing those values on the combo box as dropdown values in my lwc component. – Ruklani Attanyake Jan 18 at 12:16
1

I updated the get creditValues() as follows. After splitting the String seperated by commas, I pushed the resulting array into a new array.

 get creditValues() {
        var credit;
        var credit_val_list = [];
        credit = getFieldValue(this.record.data, CREDIT_VALUES);
        credit_val_list = String(abcd).split(";");
        console.log("Credit val" + credit_val_list);

        var i;
        var options = [];
        for (i = 0; i < credit_val_list.length; i++) {
            var item = {
                label: credit_val_list[i],
                value: credit_val_list[i]
            };
            options.push(item)

        }
        return options;


    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.