0

I am trying to populate a list of contacts from the Wrapper class.However, I am not getting any values to display in the Combo box. Here are my codes

Apex Controller Class:

public with sharing class AccountContactController {
    @AuraEnabled(Cacheable = true)
        public static List<ContactListWrapper> fetchAccountContact(string accId) {
            List<ContactListWrapper> accConLstWrapper = new List<ContactListWrapper>();
            //Map accounts
            Map<Id, Account> accMap = new Map<Id, Account>([SELECT Id, Name, ParentId FROM Account WHERE RecordTypeId = '0127F000000K6XIQA0' and id IN
                                               (SELECT AccountId FROM AccountContactRelation WHERE isSalesContract__c = true and AccountId =: accId)]);
            //Map contacts
            Map<Id, Contact> conMap = new Map<Id, Contact>([SELECT Id, Name FROM Contact WHERE id IN
                                               (SELECT ContactId FROM AccountContactRelation WHERE isSalesContract__c = true and AccountId =: accId)]);
            //Create junction table list to iterate
            List<AccountContactRelation> acrList = [SELECT AccountId, ContactId FROM AccountContactRelation WHERE isSalesContract__c=true and accountId =: accId];

            //Initialise and populate Contact List Wrapper List
            for(AccountContactRelation acr : acrList){

                accConLstWrapper.add(new ContactListWrapper(accMap.get(acr.AccountId),conMap.get(acr.ContactId)));
            }
            system.debug('Wrapper: '+accConLstWrapper);
            return accConLstWrapper;
    }
        //Lightning Combo-box Input


        //Wrapper Class
        public class ContactListWrapper {
            @AuraEnabled public Account objAcc;
            @AuraEnabled public Contact objCons;

            public ContactListWrapper(Contact con){
                this.objCons = con;
            }
            public ContactListWrapper(Account acc, Contact con) {
                this.objAcc = acc;
                this.objCons = con;
            }
        }
}

JS file

import { LightningElement, api, track, wire } from "lwc";
import fetchAccountContact from '@salesforce/apex/AccountContactController.fetchAccountContact';
import fetchPickListValue from '@salesforce/apex/AccountContactRelationController.fetchPickListValue';
let i = 0;
export default class CreateOfferTest extends LightningElement {
    //This captures AccountId which is passed from Shipping Component
    @api recordId;
    @track AccWithCons;
    //this will hold key, value pair
    @track items = [];
    //Initialize combo box value
    @track value = '';
    //This holds errors
    @track error;
    // Get the Page Id to the controller class
    @wire(fetchAccountContact, { accId: '$recordId' })
    wiredfetchAccountContact({ error, data }) {
        if (data) {
            this.AccWithCons = data;
        } else if (error) {
            console.log(error);
            this.error.error;
        }
    }
    
    //Get the Contact record to be processed.
    wiredContacts({ error, data }) {
        if (data) {
            //create array with elements which has been retrieved controller
            //here value will be Id and label of combo box wil be name
            for (i = 0; i < data.length; i++) {
                this.items = [...this.items, { value: data[i].Id, label: data[i].Name }];
            }
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.contacts = undefined;
        }
    }
    //getter property from statusOptions wich return the items array
    get statusOptions() {
        console.log(this.items);
        return this.items;
    }
    handleChange(event) {
        //Get the string of "value" attribute on the selected option
        const selectedOption = event.detail.value;
        console.log('selectedOption=' + selectedOption);
        console.log('selectedOption=' + selectedOption);
        //This is for event propagation
        const filterChangeEvent = new CustomEvent('filterchange', { detail: { selectedOption }, });
        //Fire the custom event
        this.dispatchEvent(filterChangeEvent);
    }
}

Html file

<template>
    <lightning-card title="Contact" icon-name="standard:contact">
        <lightning-combobox name="Contacts" label="Contacts" placeholder="Choose Contact" value={value}
            onchange={handleChange} options={statusOptions}>
        </lightning-combobox>
    </lightning-card>
</template>

1 Answer 1

0

Never mind I've figured it out..It's to do with accessing correct object in the multi-dimension array.. here is the code..

Snippet of the JS code..

@wire(fetchAccountContact, { accId: '$recordId' })
wiredContacts({ error, data }) {
    if (data) {
        console.log(data);
        //create array with elements which has been retrieved controller
        //here value will be Id and label of combo box wil be name
        for (i = 0; i < data.length; i++) {
            this.items = [...this.items, { value: data[i].objCons.Id, label: data[i].objCons.Name }];

        }

You must log in to answer this question.

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