[SalesForce] Lightning Components: Why Geolocation fields in SOQL lead to an Internal Server Error? Salesforce BUG

Scenario:

After I add a geolocation custom field to my SOQL-Query, I get an Internal Server Error. When I remove it, the error disappears. I'm not using the field, it's just part of the SOQL-query. The behavior is perfectly reproducible – see the very simple source-code below.

Why I'm asking?

Important: I'm not interested at all to do something with the geolocation field, so I'm not looking for any kind of workaround. I need to clarify only the background on WHY THIS IS HAPPENING. I assume a BUG, possibly in the serialization/deserialization mechanisms behind the curtains.
The reason why I get geolocation fields in my SOQL is because in my real scenario I need to use Schema methods to query ALL fields of an SObject. Then among other fields these geolocation fields (if present) are slipping in and messing up the functionality. I don't want to filter them out, because I can't see the reason why they do any harm. Lightning Components should be immune to the existence of these fields in SOQL-queries. There should be no Internal Server Error.

Prerequisite

On Account, create a custom field of type Geolocation and set it's API Name to elfMapGeolocation__c

Component:

<aura:component controller="bugLC_geolocation" implements="flexipage:availableForAllPageTypes,force:hasRecordId" access="global" >
    <aura:handler name="init" value="{!this}" action="{!c.init}" /> 
    <aura:attribute name="RecordName" type="String"  />
    TEST<br/>
    {!v.RecordName}<br/>
</aura:component>

Controller:

({
    init : function(cmp, evt, hlp) {
        console.log('init', 1 ); 
        var action = cmp.get("c.loadRecord"); 
        var accountId = '';
        action.setParams({
            recordId : cmp.get('v.recordId')
        });
        action.setCallback(this, function (res) {
            var state = res.getState();
            if(state == 'SUCCESS') {
                console.log('bugLC_geolocation.loadRecord() :: SObject=', res.getReturnValue() ); 
                cmp.set("v.RecordName", action.getReturnValue().Name );
            } else { console.log('ERROR',res.getError()); }
        });
        $A.enqueueAction(action); 
    }
})

APEX-Controller:

public class bugLC_geolocation { 
    @AuraEnabled public static Account loadRecord(Id recordId) {
        return (Account)Database.query('select Id, Name, elfMapGeolocation__c from Account where Id = :recordId')[0];
    }
}

Error in the browser console

Error: An internal server error has occurred Error ID:
453913918-140784 (-1016346842) at T.Qg
(https://xe310-dev-ed.lightning.force.com/auraFW/javascript/Wm6qgqx83KGKvMBa3vagbw/aura_prod.js:513:370)
at Object.onXHRReceived
(https://xe310-dev-ed.lightning.force.com/components/instrumentation/beacon.js:6:206)
at Object.HH.ek
(https://xe310-dev-ed.lightning.force.com/auraFW/javascript/Wm6qgqx83KGKvMBa3vagbw/aura_prod.js:220:255)
at pM.Do
(https://xe310-dev-ed.lightning.force.com/auraFW/javascript/Wm6qgqx83KGKvMBa3vagbw/aura_prod.js:873:391)
at Object.HH.ek
(https://xe310-dev-ed.lightning.force.com/auraFW/javascript/Wm6qgqx83KGKvMBa3vagbw/aura_prod.js:220:255)
at HH.start
(https://xe310-dev-ed.lightning.force.com/auraFW/javascript/Wm6qgqx83KGKvMBa3vagbw/aura_prod.js:219:313)
at XMLHttpRequest.e
(https://xe310-dev-ed.lightning.force.com/auraFW/javascript/Wm6qgqx83KGKvMBa3vagbw/aura_prod.js:507:103)
at XMLHttpRequest.b
(https://xe310-dev-ed.lightning.force.com/auraFW/javascript/Wm6qgqx83KGKvMBa3vagbw/aura_prod.js:820:83)

Verification

If you just omit the geoloaction field from the APEX-Controller, it works flawlessly:

return (Account)Database.query('select Id, Name from Account where Id = :recordId')[0];

Appendix

Even if the documentation could be interpreted as if Compound Geolocation Fields could not be queried by APEX at all, I have experienced that it works without problems e.g. in Visualforce context or in execute anonymous context this works without error

    bugLC_geolocation.loadRecord('ANY_VALID_ID');

In contradiction to that the documentation here https://help.salesforce.com/articleView?id=custom_field_geolocate_overview.htm&type=0 says:

you can’t select the compound geolocation field in Apex. You can run SOQL queries only on a geolocation field’s components.

Not sure if here again the golden rule applies: if not explicitly documented, don't use it

Best Answer

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.

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.

for more better understanding of Geolocation field limitation and usage read this article: Geolocation Custom Field | Salesforce

Related Topic