Retrieving Geolocation fields from a Lightning web component using getRecord()

added based on comments

It seems there is a bug for importing geolocation fields (able to import other compound fields like BillingCity). So, we can use direct string notation as below.


Geolocation type fields are Compound Fields (like BillingAddress). Two main points to be highlighted from docs is :

Geolocation is a compound field that counts toward your org’s limits as three custom fields: one for latitude, one for longitude, and one for internal use. Support for the compound field (geolocation) versus the field’s components (latitude and longitude) varies depending on the functionality you’re using in Salesforce. For example, you can create list views that show the field and its components, but you can’t select the compound geolocation field in Apex. You can run SOQL queries only on a geolocation field’s components.

Compound fields are accessible only through the SOAP and REST APIs. The compound versions of fields aren’t accessible anywhere in the Salesforce user interface.

Below is the working sample code:

import { LightningElement, wire, api, track } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

export default class Poc extends LightningElement {
    @api recordId;

    @wire(getRecord, {
        recordId: '$recordId',
        fields: [ 'Account.Acc_Location__Latitude__s', 'Account.Acc_Location__Longitude__s' ]
    })
    wiredAcc({ data, error }) {
        console.log('Account => ', JSON.stringify(data), JSON.stringify(error));
    }
}

and its output:

{
  "apiName": "Account",
  "childRelationships": {

  },
  "fields": {
    "Acc_Location__Latitude__s": {
      "displayValue": null,
      "value": 1.2345678
    },
    "Acc_Location__Longitude__s": {
      "displayValue": null,
      "value": 2.3456789
    }
  },
  "id": "00128000009j45sAAA",
  "lastModifiedById": "00528000001IIBvAAO",
  "lastModifiedDate": "2019-08-25T14:37:49.000Z",
  "recordTypeInfo": null,
  "systemModstamp": "2019-08-25T14:37:49.000Z"
}

Note that you should ideally use imported fields instead of direct strings in parameter