0
import PARTICIPANT_FIELD from '@salesforce/schema/User.Contact.Active_Participant__c';

// LWC Code

@wire(getRecord, { recordId: USER_ID, fields: [PARTICIPANT_FIELD] })
wiredUser({ error, data }) {
  if (data) {
    this.participantRecordId = data.fields.Contact.value.fields[ACTIVE_PARTICIPANT_API.fieldApiName].value;
    this.getParticipantStatusOutcome();
  } else if (error && USER_CONTACT_ID !=undefined) {
    showLog(error);
    showErrorMessage( 'Error', error.body.message);
  }
}

So in this block of code PARTICIPANT_FIELD will have User's contact field Active_Participant__c but in some cases the contact is blank on User object due to which it gives me the error since data is not rendered here and it contains undefined.

Is it possible that for some users even though contact lookup on User is blank the error message should not be displayed? I cannot remove showErrorMessage( 'Error', error.body.message); function here. I tried with various IF conditions but still I get the error message, may be if data is undefined then error object will have value which is being shown in UI.

I need that error message shown from toast should not be shown even though Contact is blank on User record.

2 Answers 2

0

You need to check that data.fields.Contact.value.fields[ACTIVE_PARTICIPANT_API.fieldApiName] is defined before you can get it's .value

if ( data.fields.Contact.value.fields[ACTIVE_PARTICIPANT_API.fieldApiName] ) {
    this.participantRecordId = data.fields.Contact.value.fields[ACTIVE_PARTICIPANT_API.fieldApiName].value;
} else {
    this.participantRecordId = null;
}
2
  • Don't you need to check that data.fields.Contact is defined before you can get it's value as well?
    – Nick C
    Commented Oct 21, 2021 at 1:24
  • I guess, technically, it would be a good idea - however, it's a standard response object that should always have data.fields.Contact populated.
    – Paul N
    Commented Oct 21, 2021 at 17:25
0

Official Documentation suggests using the getFieldValue function to get individual field data.

import { LightningElement, wire } from "lwc";
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import PARTICIPANT_FIELD from '@salesforce/schema/User.Contact.Active_Participant__c';

export default class MyComponent extends LightningElement {
 @wire(getRecord, { recordId: USER_ID, fields: [ PARTICIPANT_FIELD ] })
 contactData;

 get participantRecordId() {
   return getFieldValue(this.contactData.data, PARTICIPANT_FIELD);
 }
}

https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_wire_adapters_record

In your specific use case (where you're checking for if data is there or not), it might be better to keep it similar to how you have implemented it so far and just use the getFieldValue method in the @wire response.

import { getRecord, getFieldValue } from 'lightning/uiRecordApi';

// LWC code
@wire(getRecord, { recordId: USER_ID, fields: [ PARTICIPANT_FIELD ] })
wiredUser({ error, data }) {
  if (data) {
    this.participantRecordId = getFieldValue(data.fields.Contact, PARTICIPANT_FIELD);
    this.getParticipantStatusOutcome();
  } else if (error && USER_CONTACT_ID != undefined) {
    showLog(error);
    showErrorMessage( 'Error', error.body.message);
  }
}

You must log in to answer this question.

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