TransWikia.com

Wire is not calling

Salesforce Asked by Eternal09 on November 28, 2021

I tried to run this wired function when I get recordId but I am not able to run this function. Can anyone tell me what I am doing wrong here?

 import { LightningElement, wire, api } from 'lwc';
 import { subscribe, unsubscribe, MessageContext, publish } from 'lightning/messageService';
 import BOATMC from '@salesforce/messageChannel/BoatMessageChannel__c';
  import { getRecord } from 'lightning/uiRecordApi';
  import LONGITUDE_FIELD from '@salesforce/schema/Boat__c.Geolocation__latitude__s';
 import LATITUDE_FIELD from '@salesforce/schema/Boat__c.Geolocation__longitude__s';
const BOAT_FIELDS = [LONGITUDE_FIELD, LATITUDE_FIELD];
export default class BoatMap extends LightningElement {


subscription = null;
boatId;    

@api get recordId() {    
  return this.boatId;
}
set recordId(value) {
  console.log('boatId',value);
  this.setAttribute('boatId', value);
  this.boatId = value;
}


error = undefined;
mapMarkers = [];

// Initialize messageContext for Message Service
@wire(MessageContext)
messageContext;

@wire(getRecord, { recordId: '$boatId', fields: BOAT_FIELDS })
wiredRecord({ error, data }) {
  // Error handling
  if (data) {
    console.log('data',data);
    this.error = undefined;
    const longitude = data.fields.Geolocation__Longitude__s.value;
    const latitude = data.fields.Geolocation__Latitude__s.value;
    this.updateMap(longitude, latitude);
  } else if (error) {
    this.error = error;
    console.log('error',error);
    this.boatId = undefined;
    this.mapMarkers = [];
  }
}


connectedCallback() {
  
  if (this.subscription || this.recordId) {
    return;
  }
  this.subscription = subscribe(this.messageContext, BOATMC, (message) => {        
    this.recordId = message.recordId;       
  });
 
}


updateMap(Longitude, Latitude) { 
  this.mapMarkers = [{
    location:{
      latitude :Latitude,
      longitude:Longitude
    }
  }];
}


get showMap() {
  return this.mapMarkers.length > 0;
}

}

2 Answers

You can't import references for geolocation fields, you have to use string syntax.

See LWC Developer Guide -> Work with Salesforce Data -> Use the Wire Service to Get Data -> Understand the Wire Service -> Import References for Compound Fields

Answered by MikeA on November 28, 2021

You need to use $recordId as the dynamic, reactive parameter to the wire. This will operate correctly against your API property "recordId" which you implemented with getter and setter. Alternatively, make boatId tracked to use $boatId in the wire.

Also, remove the this.setAttribute('boatId', value); call in the set recordId. There's no good reason for this that I can see, unless you have a good reason for it. (This rather implies that you can just switch back to using @api recordId; too.)

Answered by Phil W on November 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP