0

I am currently trying to pass a variable from a client-side(javascript)controller to server-side(APEX)controller. I'm currently using setParams() in the client-side controller to set the current user's geolocation in the server-side controller so that I can find all accounts within 50 miles of them, but I keep getting errors such as "unexpected token 'userLat'". I have been working on figuring this out all day, but to no avail so some help would be great. Thanks in advance.

Some code:

Server-Side

public with sharing class AccountController { public String userLat {get; set; } public String userLong {get; set; }

@AuraEnabled
public static List<Account> findAll(String userLat, String userLong) {
    return [SELECT id, name, latlng__Latitude__s, latlng__Longitude__s
            FROM Account
            WHERE DISTANCE(latlng__c, GEOLOCATION(userLat, userLong), 'mi') < 50
            ORDER BY DISTANCE(latlng__c, GEOLOCATION(userLat, userLong), 'mi')
            LIMIT 5];
}

}

Client-Side:

//where I get/set the user's location - userLat and userLong var action = component.get("c.findAll"); action.setParams({"userLat": "userLat", "userLong": "userLong"}); action.setCallback(this, function(a) { component.set("v.accounts", a.getReturnValue()); var event = $A.get("e.c:AccountsLoaded"); event.setParams({"accounts": a.getReturnValue()}); event.fire(); }); $A.enqueueAction(action); }

1
  • Did you forget the Bind : for the UserLat in the SOQL query. Is that even allowed? If not, try with a dynamic query
    – Eric
    Jul 27, 2015 at 19:55

1 Answer 1

0

From what I understand you cannot use bind variables as part of the GEOLOCATION:

https://developer.salesforce.com/forums/?id=906F0000000AdhCIAS

so you will have to do it dynamically:

String q = 'SELECT id, name, latlng__Latitude__s, latlng__Longitude__s ' +
            'FROM Account ' +
            'WHERE DISTANCE(latlng__c, GEOLOCATION(' + userLat + ',' +  userLong + '),' \'mi\') < 50 ' +
            'ORDER BY DISTANCE(latlng__c, GEOLOCATION(' + userLat + ',' +  userLong + '),' \'mi\') ' +
            'LIMIT 5';
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .