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 (likeBillingAddress
). Two main points to be highlighted from docs is :Below is the working sample code:
and its output:
Note that you should ideally use imported fields instead of direct strings in parameter