Hi Folks,
For LWC Super badge I am implementing simple scenario
Scenario : To fetch get single boat record using getRecord wire adapter
Problem: wire adapter code is not getting executed
code:
import { LightningElement,wire,api } from 'lwc';
import { MessageContext, subscribe} from 'lightning/messageService';
import BOATMC from '@salesforce/messageChannel/BoatMessageChannel__c';
import { getRecord } from 'lightning/uiRecordApi';
import BOAT_LOCATION_LONGITUDE from '@salesforce/schema/Boat__c.Geolocation__Longitude__s';
import BOAT_LOCATION_LATITUDE from '@salesforce/schema/Boat__c.Geolocation__Latitude__s';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class BoatMap extends LightningElement {
@wire(MessageContext)
messageContext;
@api recordId;
mapMarkers = [];
boatId;
@wire(getRecord, { recordId: '$boatId', fields: [ BOAT_LOCATION_LATITUDE, BOAT_LOCATION_LONGITUDE] } )
wiredBoatData ({error, data}) {
if (error) {
console.log('Error occured while getting record');
this.dispatchEvent(
new ShowToastEvent({
title: 'Error',
message: error,
variant: 'error'
})
);
} else if (data) {
console.log('Data fetched :',data);
this.updateMap(data);
}
}
connectedCallback() {
subscribe(this.messageContext,BOATMC,(message)=>{
this.handleMessage(message);
})
}
handleMessage(message){
console.log('Published Message is:',message.recordId);
this.boatId=message.recordId;
console.log('this.boatId:',this.boatId);
}
updateMap(boatData){
const newMarkers = boatData.map(boat => {
return {
location: {
Latitude: boat.Geolocation__Latitude__s,
Longitude: boat.Geolocation__Longitude__s
},
title : boat.Name,
};
});
this.mapMarkers=newMarkers;
console.log('this.mapMarkers :',this.mapMarkers);
}
}
Note: Even if I pass hardcoded value of boatID to getRecord adapter it doesn't work.
I observed while debugging that wire adapter code is not getting executed but I am not understanding why ?
Can anyone suggest something else ?
We need to direct import the geolocation field like this:
const FIELD_Latitude = "Boat__c.Geolocation__Latitude__s";
const FIELD_Longitude = "Boat__c.Geolocation__Longitude__s";
const BOAT_FIELDS = [FIELD_Latitude, FIELD_Longitude];
Working example:
const FIELD_Latitude = "Boat__c.Geolocation__Latitude__s";
const FIELD_Longitude = "Boat__c.Geolocation__Longitude__s";
import { LightningElement, wire } from "lwc";
import { getRecord } from "lightning/uiRecordApi";
import { MessageContext, subscribe } from "lightning/messageService";
import BOATMC from "@salesforce/messageChannel/BoatMessageChannel__c";
const BOAT_FIELDS = [FIELD_Latitude, FIELD_Longitude];
export default class BoatMap extends LightningElement {
// private
subscription = null;
boatId;
@wire(MessageContext)
messageContext;
error = undefined;
mapMarkers = [];
// Getting record's location to construct map markers using recordId
// Wire the getRecord method using ('$boatId')
@wire(getRecord, { recordId: "$boatId", fields: BOAT_FIELDS })
wiredRecord({ error, data }) {
// Error handling
console.log("boatMap wiredRecord anuj", data, error, this.boatId);
if (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;
this.boatId = undefined;
this.mapMarkers = [];
}
}
subscribeMC() {
this.subscription = subscribe(this.messageContext, BOATMC, (message) =>
this.handleMessage(message)
);
}
// Handler for message received by component
handleMessage(message) {
this.boatId = message.boatId;
console.log("boatMap handleMessage anuj", message, this.boatId);
}
// Calls subscribeMC()
connectedCallback() {
console.log("boatMap connectedCallback");
this.subscribeMC();
}
// Creates the map markers array with the current boat's location for the map.
updateMap(Longitude, Latitude) {
console.log("boatMap updateMap", Longitude, Latitude);
this.mapMarkers = [
{
location: {
Latitude: Latitude,
Longitude: Longitude
}
}
];
}
// Getter method for displaying the map component, or a helper method.
get showMap() {
return this.mapMarkers.length > 0;
}
}