5

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';
7

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

| improve this answer | |
  • 1
    I have accepted your answer as you have provided an excellent bit of new information - that querying the __s fields by name rather than importing them does work and eliminates the need for formula fields. – Joe Flowers Aug 26 '19 at 16:03
  • However, I wanted to also ask if you ran into the same error I did when trying to import the __s fields ... I am unable to query the imported latitude__s and longitude__s fields, have you had any luck? I am excluding the compound field from the query. – Joe Flowers Aug 26 '19 at 16:06
  • 1
    Am also getting error - modified answer. – salesforce-sas Aug 26 '19 at 16:32
  • Thanks, much appreciated! – Joe Flowers Aug 26 '19 at 18:05
  • This is now documented in the LWC docs and I've added an update to the question reflecting that. – Joe Flowers Dec 9 '19 at 2:19
0

The imports can be replaced as shown below:

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

/** My code goes here*/
import { getRecord } from 'lightning/uiRecordApi';
const FIELD_Latitude ='Photo__c.Geolocation__Latitude__s';
const FIELD_Longitude ='Photo__c.Geolocation__Longitude__s';
const PHOTO_FIELDS = [FIELD_Latitude,FIELD_Longitude];
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.