0

I would like to show a map of a specific location on Record Pages Since I have two objects where I would like to display this map I would like to make my component aware of its context before defining the variables for the map

I wanted to use @ObjectApiName like in the doc but every time I do it I get an error, Object API name is not defined .. I do not know what I am doing wrong...

My code looks the following:

import { LightningElement, wire, api } from 'lwc';
import { getRecord, getFieldValue } from "lightning/uiRecordApi";
import Latitude__c from "@salesforce/schema/Selina_Location__c.Latitude__c";
import Longitude__c from "@salesforce/schema/Selina_Location__c.Longitude__c";
import Latitude__c2 from "@salesforce/schema/Trip__c.Latitude__c";
import Longitude__c2 from "@salesforce/schema/Trip__c.Longitude__c";
//import Name__c from "@salesforce/schema/Selina_Location__c.Name__c";


export default class MapLocation extends LightningElement {

    @api recordId;
    @api objectApiName;
    recordData;
    mapMarkers;
    zoomLevel = 12;
    

    @wire(getRecord, {       
        recordId: "$recordId",
        fields: [Latitude__c, Longitude__c, Latitude__c2, Longitude__c2],
      })
      getLocationData(wireResult) {
        const { error, data } = wireResult;
        if (data) {
          this.recordData = data
          
          console.log('dataToCheck',this.recordData['fields'])
          console.log('dataToCheck34',this.recordData['fields']['Longitude__c']['value'])

          this.mapMarkers = [
            {
                location: {
                    Latitude: this.recordData['fields']['Latitude__c']['value'],
                    Longitude: this.recordData['fields']['Longitude__c']['value'],
                },
                title: 'Ramon'
            },
        ];
        
        }
        if (error) {
          console.log(objectApiName);
          this.error = error;
        }
      }

     

    
    
}
3
  • Are you doing this in a Digital Experience ("community") site page or in a Salesforce UI page?
    – Phil W
    Commented Aug 4, 2022 at 8:02
  • Nop Lightning Web Component on a custom Object
    – Ben2pop
    Commented Aug 4, 2022 at 8:33
  • So in a Lightning Record Page (i.e. a Salesforce UI page, not a Digital Experience one)...
    – Phil W
    Commented Aug 4, 2022 at 9:18

2 Answers 2

1

In order to use an instance variable, like objectApiName, you must access it via this, so you have to change the console log to:

console.log(this.objectApiName);

Keep in mind that Javascript is case-sensitive, so objectApiName and ObjectApiName are not the same.

1
  • Well spotted ;) My comment question on the original was asked because Digital Experiences don't automatically make the objectApiName available, but that wasn't relevant here.
    – Phil W
    Commented Aug 4, 2022 at 14:57
0

If You Are Using Global variables then you have to use 'this' keyword with the variable Name. For Example : Other than this -- console.log(objectApiName); Use this -- console.log(this.objectApiName);

You must log in to answer this question.

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