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})


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

  • What do you want the recordId to be in this case?
    – Lance Shi
    Aug 7 '19 at 11:44
  • Why is GetRecord needed on home page? Aug 7 '19 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. Aug 7 '19 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 Aug 7 '19 at 11:46
  • In that case, you can simply handle undefined in your apex I suppose?
    – Lance Shi
    Aug 7 '19 at 11:56

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.