I need to calculate distance between Lead and Account, but I get this error:

Method does not exist or incorrect signature: void GEOLOCATION(Decimal, Decimal)

My code:

for(Lead ld:[Select Id,City,State,SIC_Code_1__c,Latitude,Longitude from Lead where Id=:CurrentId ])
if(Siccodes !=null && !Siccodes.isEmpty()){
    for (Opportunity opp:[Select Id,Name,StageName,Account.name,Number_of_Units__c,account.SIC_Code__c,Type,
                          account.No_of_active_units__c,account.BillingLatitude,account.BillingLongitude from opportunity where account.SIC_Code__c 
                          LIKE :codes and StageName='Closed Won' ORDER BY account.No_of_active_units__c Desc NULLS LAST Limit 10])
        if(opp.account.SIC_Code__c <> null)
            Total = DISTANCE(GEOLOCATION(opp.account.BillingLatitude,opp.account.BillingLongitude),GEOLOCATION(Latitudes,Longtitudes), 'mi');


DISTANCE & GEOLOCATION are SOQL Methods, you can't use them in apex.

Thanks for your comment kurunve, so as documented here, this should work in Apex:

Location loc1 = Location.newInstance(28.635308,77.22496);
Location loc2 = Location.newInstance(37.7749295,-122.4194155);
Double dist = Location.getDistance(loc1, loc2, 'mi');
  • 2
    you can add that there is Schema.Location class, that allow to do such a things in apex developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – kurunve Oct 24 '17 at 15:37
  • Is there any possibility to use default Latitude and Longitude fields in apex to retrive distance.Because i have already billing location address & lead address .So, i need to provide distance between them – Pavan tej Oct 24 '17 at 15:40
  • what do you mean by "default"? You are already using the standard fields like account.BillingLatitude – Basti Oct 24 '17 at 15:42
  • @Basti why i am getting null if i try to query Location loc1 = Location.newInstance(Latitudes,Longtitudes); System.debug('Data in:'+loc1); Location loc2 =Location.newInstance(opp.account.BillingLatitude,opp.account.BillingLongitude); System.debug('Data in loc2:'+loc2); – Pavan tej Oct 24 '17 at 16:40
  • debug all your variables, some of them might be empty? – Basti Oct 24 '17 at 16:41

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.