I'm trying to create a SOQL query to retrieve Contacts from Accounts, with the contacts for each account sorted by the distance between the contact location and the account location.
This is what I have so far, but when I try to run it, I get an error stating
"unexpected token: 'account.locationField__c'".
Any ideas on what I'm doing wrong or how I can restructure this to get it to work?
Thanks!
List<Account> accounts = [
SELECT Id, Account.locationField__c, (
SELECT Id, accountId, Contact.locationField__c
FROM Contacts
WHERE RecordType.Name = 'Customers'
ORDER BY DISTANCE(Contact.locationField__c, Account.locationField__c, 'mi')
)
FROM Account
];
Also I've tried using a GEOLOCATION
and it does not work with the relationship fields but it works fine with static numbers:
Works! (Static coordinates)
List<Account> accounts = [
SELECT Id, Account.locationField__c, (
SELECT Id, accountId, Contact.locationField__c
FROM Contacts
WHERE RecordType.Name = 'Customers'
ORDER BY DISTANCE(
Contact.locationField__c,
GEOLOCATION(33.153, -112.616),
'mi'
)
)
FROM Account
];
Does NOT work! (Account Relationship fields)
List<Account> accounts = [
SELECT Id, Account.locationField__c, (
SELECT Id, accountId, Contact.locationField__c
FROM Contacts
WHERE RecordType.Name = 'Customers'
ORDER BY DISTANCE(
Contact.locationField__c,
GEOLOCATION(Account.latitudeField__c, Account.longitudeField__c),
'mi'
)
)
FROM Account
];
I've also referenced the relationship field like so:
Contact.Account.CustomFields
Outside the context of DISTANCE/GEOLOCATION
this relationship statements work fine but within it they return as an unexpected token.