function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Piotr BPiotr B 

get records adapter issue

Hi guys so im facing issue with getRecords adapter when i hard code to adapter configuration Ids of records it works but when i send a array of string as it is requierd data is not fetching 

 

import { LightningElement, api, wire, track } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { getRecords } from 'lightning/uiRecordApi';
import getResult from '@salesforce/apex/CustomUser.getResult';
const FIELDS = [
    'Result__c.Name',
    'Result__c.User__c',
    'Result__c.License_Proposal__c',
    'Result__c.Current_License__c'
];
const COLS = [
    { label: 'Id', fieldName: 'Id' },
    { label: 'Email', fieldName: 'Email' },
    { label: 'Current License', fieldName: 'Current License' },
    { label: 'License Proposal', fieldName: 'License Proposal' }
];
export default class TestCmp extends LightningElement {
    @api recordId;
    contacts;
    @track records = [];
    columns = COLS;
    testing = [];
    @wire(getResult)
    resultHandler({ data, error }) {
        if (data) {
            console.log(data);
            this.records = data;
            console.log(Array.isArray(this.records));
        } else if (error) {
            console.error(error);
        }
    }
    @wire(getRecords, {
        records: [
            {
                recordIds: '$records',
                fields: FIELDS
            }
        ]
    })
    wiredRecord({ error, data }) {
        if (error) {
            let message = 'Unknown error';
            if (Array.isArray(error.body)) {
                message = error.body.map((e) => e.message).join(', ');
            } else if (typeof error.body.message === 'string') {
                message = error.body.message;
            }
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error loading Result',
                    message,
                    variant: 'error'
                })
            );
        } else if (data) {
            console.log(data);
            this.contacts = data.results.map((item) => {
                return {
                    Id: this.getValue(item, 'User__c'),
                    Email: this.getValue(item, 'Name'),
                    'Current License': this.getValue(
                        item,
                        'Current_License__c'
                    ),
                    'License Proposal': this.getValue(
                        item,
                        'License_Proposal__c'
                    )
                };
            });
            console.log('i m here ' + this.contacts);
            // this.name = this.contacts.results[1].result.fields.Name.value;
            // this.phone = this.contacts.results[1].result.fields.Phone.value;
        }
    }
    getValue(data, field) {
        return data.result.fields[field].value;
    }

But if i give it ['a0*********QAU', 'a06**********AU']  instead of array of '$records' it works  

Vinay MVinay M

Hi piotr Bęben 8,

   The array you are passing is dependent on other wire. We cannot control the order of execution when there is more than one wire method. This is the reason why it works when you hardcode Ids. I would suggest to run the from connected callback (create a method whcih will run apex imperatively, and call this method in connected call back). This way you may have apex running before the wire.

Thank you,

Vinay.

Piotr BPiotr B
Hi vinay m, i tried your suggestion  and this is still not working ,datatable  shows without data  
    @api recordId;
    contacts;
    @track records = [];
    columns = COLS;
    testing = [];
    @track errors;
    connectedCallback() {
        console.log('conectedCallback');
        getResult()
            .then((result) => {
                this.records = result;
            })
            .catch((error) => {
                this.errors = error;
            });
    }
    @wire(getRecords, {
        records: [
            {
                recordIds: '$records',
                fields: FIELDS
            }
        ]
    })
    wiredRecord({ error, data }) {
        if (error) {
            let message = 'Unknown error';
            if (Array.isArray(error.body)) {
                message = error.body.map((e) => e.message).join(', ');
            } else if (typeof error.body.message === 'string') {
                message = error.body.message;
            }
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error loading Result',
                    message,
                    variant: 'error'
                })
            );
        } else if (data) {
            console.log(data);
            this.contacts = data.results.map((item) => {
                return {
                    Id: this.getValue(item, 'User__c'),
                    Email: this.getValue(item, 'Name'),
                    'Current License': this.getValue(
                        item,
                        'Current_License__c'
                    ),
                    'License Proposal': this.getValue(
                        item,
                        'License_Proposal__c'
                    )
                };
            });
            console.log('i m here ' + this.contacts);
            // this.name = this.contacts.results[1].result.fields.Name.value;
            // this.phone = this.contacts.results[1].result.fields.Phone.value;
        }
    }
    getValue(data, field) {
        return data.result.fields[field].value;
    }
 
Vinay MVinay M

Try using  " this.records " instead of " $records " in wire adapter. Also, check to see if the apex call in connected callback is returning data (only Ids). 

Thank you,

Vinay.