0

I am making a Lightning Web Component and for it I want to populate a dual list box with a list of records, with the value of one field being the label and the ID of the record as the value. I am using wire to pull the records via an Apex controller but really struggling to figure out how I can subsequently loop over that to push the two values into both label and value for the dual list box options.

JS

import { LightningElement, wire } from 'lwc';
import getdualRecords from '@salesforce/apex/DualListController.records';

export default class scratchtest extends LightningElement {

    _selected = [];

    options = [];
    @wire(getdualRecords , { email: '[email protected]' })
    recordresults({data,error}) {
        data && this.options = data.map(record => ({value: record.Id, label: record.Name}));
    }

    get selected() {
        return this._selected.length;
    }

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

HTML

<template>
    <lightning-card>
        <div class="slds-p-around_small">
            <lightning-dual-listbox 
                name="testduallist"
                label="Select Records"
                source-label="Available Records"
                selected-label="Linked Records"
                field-level-help="Select records"
                options={options}
                onchange={handleChange}
                disable-reordering=true
            ></lightning-dual-listbox>
        </div>
    </lightning-card>
</template>

Apex

public class DualListController{
    @AuraEnabled(cacheable=true)
    public static List<Object__c> records(String email) {
        return  [
            SELECT Id, Name
            FROM Object__c
            WHERE Email__c = :email AND Active__c = TRUE
            WITH SECURITY_ENFORCED
        ];
    }
}
2
  • Can you show what you've done so far?
    – gvgramazio
    Commented Nov 26, 2021 at 16:17
  • Added in relevant snippets of code with attempt at below solution
    – Jonathan
    Commented Nov 26, 2021 at 17:00

1 Answer 1

0

You can use a wire handler for this purpose:

options = [] // List of options for dual list box
@wire(getRecords, {}) getRecordsResult({data,error}) {
  data && (this.options = data.map(record => ({ value: record.Id, label: record.Name })))
}

We use Array.prototype.map to convert the incoming data from normal record format into a format suitable for the dual list box's options. Note that @track is not necessary when we use this technique, as the system detects that we have replaced the old Array object with a new Array object, and thus triggers a render cycle.

3
  • I tried this and I get 'Invalid left-hand side in assignment expression' error. Also added relevant snippets of code to question
    – Jonathan
    Commented Nov 26, 2021 at 17:00
  • @NewToThis There was just a missing set of parentheses is all. Try the new version.
    – sfdcfox
    Commented Nov 26, 2021 at 17:21
  • Thank you so much, that mapping function is so useful. Works perfectly now
    – Jonathan
    Commented Nov 26, 2021 at 19:44

You must log in to answer this question.

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