0

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

}

1

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

1
  • I tried both ways as you describe in your answer but still no result nothing is happening – Eternal09 Jul 22 '20 at 17:46
0

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

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.