0

I am expecting a 400 error when I pass a malformed recordId (e.g. "random-non-sf-id") into uiRecordApi/getRecord, but I am not receiving any error as per Status Codes and Error Responses.

Question: Is there a way to tell if a wire service call has failed specifically due to bad input?

Using a barebones example, I cannot get the second conditional error template to render.

JS module:

import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

import FIRSTNAME_FIELD from '@salesforce/schema/Contact.FirstName';

const fields = [FIRSTNAME_FIELD];

export default class GetContact extends LightningElement {
    // Passed in from parent or can hardcode string as an example
    @api recordId; 

    @wire(getRecord, { recordId: '$recordId', fields })
    contact;
}

HTML template:

<template if:true={contact.data}>
    <p>Contact details</p>
</template>
<template if:true={contact.error}>
    <p>Error message</p>
</template>
2
  • have you tried inspecting {contact.error.message} or {contact.error.body.message}? Commented Mar 6, 2020 at 23:28
  • {contact.error} is always undefined in the above example.
    – akiradev
    Commented Mar 7, 2020 at 9:30

1 Answer 1

0

It turns out I was completely barking up the wrong tree with my original answer.

The real issue you're running into is that on initial render, contact.error is falsy and contact.data is falsy. Since you don't reference their value anywhere on the page, the engine doesn't know it needs to recalculate their values.

If you simply reference their values in the view (not just in the template tag) as seen here:

<template>
  <template if:true={contact.data}>
    <p>{contact.data} Contact Details</p>
  </template>
  <template if:true={contact.error}>
    <p>{contact.error} Error Message</p>
  </template>
</template>

Everything will start rendering and re-rendering as expected.

The only thing on your controller code is I hardcoded an Id.

import { LightningElement, api, wire } from "lwc";
import { getRecord } from "lightning/uiRecordApi";

import FIRSTNAME_FIELD from "@salesforce/schema/Contact.FirstName";

const fields = [FIRSTNAME_FIELD];

export default class GetContact extends LightningElement {
  //@api recordId;
  //fake id
  recordId = "fake";

  @wire(getRecord, { recordId: "$recordId", fields })
  contact;
}

Referencing your desired values in getters as seen in the documentation at https://developer.salesforce.com/docs/component-library/documentation/en/48.0/lwc/reference_wire_adapters_record will also cause things to start re-rendering as expected.

For example: JS:

import { LightningElement, api, wire } from "lwc";
import { getRecord } from "lightning/uiRecordApi";

import FIRSTNAME_FIELD from "@salesforce/schema/Contact.FirstName";

const fields = [FIRSTNAME_FIELD];

export default class GetContact extends LightningElement {
  //@api recordId;
  //fake id
  recordId = "fake";

  @wire(getRecord, { recordId: "$recordId", fields })
  contact;
  //the engine now knows to track this value since I referenced it in a getter
  //even though I never use it on the page
  get error(){
      return this.contact.error;
  }
  //the engine now knows to track this value since I referenced it in a getter
  //even though I never use it on the page
  get data(){
      return this.contact.data;
  }
}

HTML:

<template>
  <template if:true={contact.data}>
    <p>Contact Details</p>
  </template>
  <template if:true={contact.error}>
    <p>Error Message</p>
  </template>
</template>
3
  • This would work in most scenarios. result.error would be set even when a valid recordId is provided. If I were to reset result.error when data is defined, the error will still appear briefly between the time the component is placed on the page and completing the wire call.
    – akiradev
    Commented Mar 10, 2020 at 15:48
  • I'm setting result.error if result.error and result.data are undefined. I'll test my code tomorrow and verify it works, but it definitely should
    – Plinderman
    Commented Mar 11, 2020 at 1:54
  • My original answer was completely wrong. Please see my edits for the real answer.
    – Plinderman
    Commented Mar 11, 2020 at 16:07

You must log in to answer this question.

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