7

I love how the LWC Wire Data Service reduces the amount of boilerplate code. I have a component which can be placed on Home AND Record pages. On Home pages there is no recordId and my call to

@wire(getRecord, { recordId: "$recordId", fields: this.fields})

fails.

Is there a way (with minimal extra code) to make it work?

9
  • What do you want the recordId to be in this case?
    – Lance Shi
    Commented Aug 7, 2019 at 11:44
  • Why is GetRecord needed on home page? Commented Aug 7, 2019 at 11:45
  • When a LWC is on a record Page a @api recordId; is populated with the current record id. It stays undefined if the same LWC is on a Home Page. Commented Aug 7, 2019 at 11:45
  • @salesforce-sas It is part of a generic component which can go on any page. I just need a way to not make parts of the code fail in such cases Commented Aug 7, 2019 at 11:46
  • In that case, you can simply handle undefined in your apex I suppose?
    – Lance Shi
    Commented Aug 7, 2019 at 11:56

1 Answer 1

9

When you implement wire service, it will always be invoked the first time when its registered while component initialisation. Then it will return { data: undefined, error: undefined }

However it will be invoked again only when dynamic variable recordId changes as wire is dependent on it. So, you can safely use if(this.recordId) inside the functions which are dependent on record pages context and handle the logic accordingly.

You must log in to answer this question.

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