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

I am attempting to retrieve the value of a Geolocation field in a Lightning web component by using wire service with the uiRecordApi's getRecord() method.

I can import the compound field or its individual pieces.

import FIELD_Location from '@salesforce/schema/Photo__c.Location__c';
import FIELD_Latitude from '@salesforce/schema/Photo__c.Location__Latitude__s';
import FIELD_Longitude from '@salesforce/schema/Photo__c.Location__Longitude__s';

However when I attempt the call to getRecord() it fails

@wire(getRecord, { recordId: '$id', fields: [FIELD_Name, FIELD_Latitude, FIELD_Longitude]})
wiredPhoto;

It populates wiredPhoto.error with a message that Location__c does not exist.

I worked around this by creating formula fields to hold latitude and longitude and I can read those using getRecord() without an issue, but that seems subpar.

Has anyone else run into the same issue? Wondering if I'm missing something obvious or if this is a known limitation.


EDIT: The requirement for not using @salesforce/schema is now documented in the LWC docs

NOTE You can’t import geolocation fields from @salesforce/schema.
Instead, to access a geolocation field or its constituent fields, use
string syntax.

// User.Place_of_birth__c is a compound custom field of type Geolocation
const PLACE_OF_BIRTH_LAT_FIELD = 'User.Place_of_birth__Latitude__s';
const PLACE_OF_BIRTH_LON_FIELD = 'User.Place_of_birth__Longitude__s';

Best Answer

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