+ Start a Discussion
Ronak PatelRonak Patel 

Use Custom Function in SOQL Query

I have one Public Function which Calculate Distance between two location

Public Integer getdistace(location1,location2).

Now i have to make one query which can ues this function in soql query to get all records which have min distance 50 km.

how to use this function in soql query?



Nilesh ManeNilesh Mane

Can you share ur code?


Even if using custom functions in SOQL are possible (but - to my knowledge - they are not), I guess it is not what you actually need. Probable better option is to precalculate the distance on inserts/updates in a trigger and store it in a field.


However it would be nice to look at your code ;)

Ronak PatelRonak Patel

I have this public Function in my controller which is calculate distance between two location from  their latitude and Longitude

Public Void distance(lat1,lat2,lat3,lat4)
double theta = lon1 - lon2;
  double dist = (Math.sin((lat1*3.14)/ 180.0) * Math.sin((lat2*3.14)/ 180.0) )+ (Math.cos((lat1*3.14)/ 180.0)) * Math.cos((lat2*3.14)/ 180.0) * Math.cos((theta*3.14)/ 180.0));
  dist = Math.acos(dist);
  dist = (dist*180.0 )/ 3.14;
  dist = dist * 60 * 1.1515;

I have to find All location from acccount where distance is < 50

So i try to use thi squery..
Double lat1=any value;
Double lat2=any value;

List<account> lst=[select id,name,latitude__c,Longitude__c from account where distance(:lat1,:lat2,latitude__c,Longitude__c) <50 ];