1

I have followed the documentation but don't understand why I receive the following error in experience builder for my component:

LWC component's @wire target property or method threw an error during value provisioning. Original error:
[Cannot read properties of null (reading 'fields')]

The component works fine when deployed - the error only appears in the builder.

Here is the homeTilesLWC.js:

import { LightningElement, wire, api, track } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import USER_ID from '@salesforce/user/Id';
import CONTACT_MANAGED_SERVICE from '@salesforce/schema/User.Contact.Managed_Services__c';
import CONTACT_IMPLEMENTATION_USER from '@salesforce/schema/User.Contact.Implementation_User__c';
import CONTACT_SALES_PERSON from '@salesforce/schema/User.Contact.Sales_Person__c';

export default class HomeTilesLWC extends LightningElement {
    @api managedServiceUser;
    @api implementationUser;
    @api salesPerson;
    showProjectsIcon;

    @api userId = USER_ID;
    @track record;
    @track error;

    @wire(getRecord, {recordId: '$userId', fields: [CONTACT_MANAGED_SERVICE, CONTACT_IMPLEMENTATION_USER, CONTACT_SALES_PERSON]})
    wiredUser({ error, data }) {
        if (data) {
            this.record = data;
            this.error = undefined;

            this.managedServiceUser = data.fields.Contact.value.fields.Managed_Services__c.value;
            this.implementationUser = data.fields.Contact.value.fields.Implementation_User__c.value;
            this.salesPerson = data.fields.Contact.value.fields.Sales_Person__c.value;

            if ((this.implementationUser == true) || (this.salesPerson == true && this.implementationUser == true)) {
                this.showProjectsIcon = true;
            }
            
        } else if (error) {
            this.error = error;
            this.record = undefined;
            console.log('error >>> ' + JSON.stringify(error));
        }
    }   
}

And homeTilesLWC.html:

<template>
    <template if:true={record}>
        LAYOUT HERE
    </template>
</template>

Could it be something to do with how I am assigning variables if there is data returned?

1 Answer 1

0

the official documentation suggest to use getFieldValue() method.
Also I would add a check if data.fields.Contact value is not null (it is common issue)!

So the next code should work for you :

import { LightningElement, wire, api, track } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import USER_ID from '@salesforce/user/Id';
import CONTACT_MANAGED_SERVICE from '@salesforce/schema/User.Contact.Managed_Services__c';
import CONTACT_IMPLEMENTATION_USER from '@salesforce/schema/User.Contact.Implementation_User__c';
import CONTACT_SALES_PERSON from '@salesforce/schema/User.Contact.Sales_Person__c';

export default class HomeTilesLWC extends LightningElement {
    @api managedServiceUser;
    @api implementationUser;
    @api salesPerson;
    showProjectsIcon;

    @api userId = USER_ID;
    @track record;
    @track error;

    @wire(getRecord, {recordId: '$userId', fields: [CONTACT_MANAGED_SERVICE, CONTACT_IMPLEMENTATION_USER, CONTACT_SALES_PERSON]})
    wiredUser({ error, data }) {
        if (data) {
            this.record = data;
            this.error = undefined;

            this.managedServiceUser = getFieldValue(data.fields.Contact, CONTACT_MANAGED_SERVICE) ? getFieldValue(data.fields.Contact, CONTACT_MANAGED_SERVICE) : null;
            this.implementationUser = getFieldValue(data.fields.Contact, CONTACT_IMPLEMENTATION_USER) ? getFieldValue(data.fields.Contact, CONTACT_IMPLEMENTATION_USER) : null;
            this.salesPerson = getFieldValue(data.fields.Contact, CONTACT_SALES_PERSON) ? getFieldValue(data.fields.Contact, CONTACT_SALES_PERSON) : null;

            if ((this.implementationUser == true) || (this.salesPerson == true && this.implementationUser == true)) {
                this.showProjectsIcon = true;
            }
            
        } else if (error) {
            this.error = error;
            this.record = undefined;
            console.log('error >>> ' + JSON.stringify(error));
        }
    }   
}

Please say if you have any questions

You must log in to answer this question.

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