0

I'm having problems to understand why the first wire is not retrieving data

I've been trying to keep this URL component in the .html as it was before my changes, so now instead of the value holding a single URL, it would retrieve a different url depending on the current campaign recordtype.

<p>Click the <u><lightning-formatted-url value={viewClientsURL} label={label.ReportLabel} target="_blank"></lightning-formatted-url></u> {label.ReportDesc}</p>

This is my .js

 @api get campId() {
    return this._campId;
  }
  set campId(campId) {
    this._campId = campId;
    this.ViewClientsURL = (this.eventsRecordTypeId === this.currentCampaignRecordType)? this.label.ViewClientsURL_Event + campId : (this.otherMarketingRecordTypeId === this.currentCampaignRecordType)?this.label.ViewClientsURL_Other + campId:this.label.ViewClientsURL + campId;

    /*
    this.viewClientsURL = this.label.ViewClientsURL + campId;//Before
    */
  }

  
  @wire(getObjectInfo, { objectApiName: CAMPAIGN_OBJECT })
  campaignInfo;

  get eventsRecordTypeId() {
    const rtis = this.campaignInfo.data.recordTypeInfos;
    return Object.keys(rtis).find(rti => rtis[rti].name === 'Event Campaign');
  }
  
  @wire(getRecord, { recordId: "$campId", fields: [CAMPAIGN_CALNOMINFO, CAL_FORM_END_DATE, CAMPAIGN_NAME, CAMPAIGN_RECORDTYPEID] })
  campaign;

  get currentCampaignRecordType(){
    return getFieldValue(this.campaign.data, CAMPAIGN_RECORDTYPEID);
  }

This is the error that I'm getting when I open the formatted link enter image description here

It's not detecting the data of campaignInfo that should be stored trough the @wire and it's breaking when I try to assign this.ViewClientsURL value conditionally trough this.eventsRecordTypeId, so it fails on set campId(). Are my thoughts right or is it something even more obvious?

2 Answers 2

2

Data from wire methods will not be available until at least one render cycle. That's how LWCs work. As such, you definitely must make sure that data is not null:

get eventsRecordTypeId() {
  if(this.campaignInfo?.data) {
    const rtis = this.campaignInfo.data.recordTypeInfos;
    return Object.keys(rtis).find(rti => rtis[rti].name === 'Event Campaign');
  }
  return null;
}
3
  • 1
    We also have historically found the wire is called first with an object with null data and null error and subsequently called with the required data (or error). Not checked in more recent releases since not developed any LWCs for a good 6 months.
    – Phil W
    Commented Sep 30, 2021 at 6:30
  • I see my problem a little better, I have two wires and both depend on each other data, but the execution order fails often. I don't see how to fix it, a null check is not gonna make it this time Commented Sep 30, 2021 at 6:55
  • @Dead_end_Developer You'll possibly need to write wire handlers to deal with the situation, or use a Promise chain. I describe these two alternative solutions to this problem here.
    – sfdcfox
    Commented Sep 30, 2021 at 9:24
0

It might be that you need to add a null check on the getter as the first call to the wire method might be bringing back a null value and the getter might be failing because its trying to do dot notation into a null value. Start by wrapping the logic in the getter like so

   if(this.campaignInfo != null){
       const rtis = this.campaignInfo.data.recordTypeInfos;
       return Object.keys(rtis).find(rti => rtis[rti].name === 'Event Campaign');
   }

If that doesn't work I would check the getObjectInfo method to figure out why no data is being passed into the campaignInfo variable

1
  • campaignInfo is actually retrieving data, something that confuses me is that eventsRecordTypeId is being called from buttons and it works fine. But unless there is a way to call a function from a hyperlink, I would still have to find the way to call that method on lwc rendering. Commented Sep 30, 2021 at 1:12

You must log in to answer this question.

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