Wednesday, April 3, 2013

Using the new Salesforce Geolocation Fields

I started with all the links I could find on Google
https://help.salesforce.com/HTSearchResults?qry=geolocation+soql
https://help.salesforce.com/HTViewSolution?id=000159817&language=en_US
https://help.salesforce.com/HTViewSolution?id=000171328&language=en_US
http://salesforce.stackexchange.com/questions/388/geolocation-searching
http://blogs.developerforce.com/engineering/2012/06/new-geolocation-features-and-mobile-apps.html

The best sample query I could find was from that last link:
SELECT name__c, phone__c
FROM restaurant__c
WHERE DISTANCE(loc__c, GEOLOCATION(37.794915,-122.394733), "mi") <= 1


 But the above did not work.  Problem?  As documented elsewhere the query only supports < or > not <=.  The correct query is:

SELECT name__c, phone__c
FROM restaurant__c
WHERE DISTANCE(loc__c, GEOLOCATION(37.794915,-122.394733), "mi") < 1



Another link that shows the correct query was found later. See
http://www.salesforce.com/us/developer/docs/soql_sosl/

Testing Queries


Next I found that my copy of SOQLExplorer doesn't support these queries at all.   I'll have to update my MAC operating system to update to the latest SOQLExplore.   But we can test these queries using the developer console.   Log onto Salesforce. Select your Name. In the drop down select Developer Console.

In the console use the Query Editor.

Using Geolocation with Partner API Queries

Next I found that my older version of the partner API does not support these queries either.  That made me try to rebuild the partner.api.  See previous posting
http://sforcehacks.blogspot.ca/2013/04/building-partnerjar-api-file.html

I'll see if this works and then come back and update this posting.

Building the Partner.jar API file

The SF documentation is missing some information needed to build the latest partner.jar API file.  Here is the link I found that gets you started
http://www.salesforce.com/us/developer/docs/api_asynch/Content/asynch_api_code_set_up_client.htm

The missing part is a dependency on a Javascript libraray called rhino.  If you follow the instructions given you get

~/Documents/workspace2/salesforce/carmanah_salesforce$ java -classpath force-wsc-27.0.0.jar com.sforce.ws.tools.wsdlc partner.wsdl partner-27.jar
[WSC][wsdlc.run:320]Created temp dir: /var/folders/D0/D0me7a5vFGmGh95S1IO9PU+++TI/-Tmp-/wsdlc-temp-3884699697501788635-dir
[WSC][wsdlc.<init>:81]Generating Java files from schema ...
Exception in thread "main" java.lang.NoClassDefFoundError: org/mozilla/javascript/Scriptable
    at com.sforce.ws.tools.TypeGenerator.generate(TypeGenerator.java:73)
    at com.sforce.ws.tools.wsdlc.generate(wsdlc.java:291)
    at com.sforce.ws.tools.wsdlc.generateTypes(wsdlc.java:278)
    at com.sforce.ws.tools.wsdlc.<init>(wsdlc.java:81)
    at com.sforce.ws.tools.wsdlc.run(wsdlc.java:320)
    at com.sforce.ws.tools.wsdlc.main(wsdlc.java:311)
Caused by: java.lang.ClassNotFoundException: org.mozilla.javascript.Scriptable
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 6 more

The solution is to get the latest rhino library from http://mvnrepository.com/artifact/rhino
and add this to the command line.

Just for the record, the WSC download is here http://mvnrepository.com/artifact/com.force.api/force-wsc
I'm working with v27 today,  file: "force-wsc-27.0.0.jar"
I also downloaded rhino jar file: "js-1.7R2.jar"

I am working on a MAC. I placed both downloaded jar files in my working directory.  I downloaded the partner.wsdl file as well.  Then use this command line to produce the partner jar file:

java -classpath force-wsc-27.0.0.jar:js-1.7R2.jar com.sforce.ws.tools.wsdlc partner.wsdl partner-27.jar

On some systems you need to use a semi-colon to separate the jar files. Like this:
java -classpath force-wsc-27.0.0.jar;js-1.7R2.jar com.sforce.ws.tools.wsdlc partner.wsdl partner-27.jar