function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Reshmi SmijuReshmi Smiju 

Database.query Exception==> unexpected token: (

Hi,

I am having trouble with the following statement , which finds the nearest  latitude and longitude of the current user place. . But I am getting the error as  unexpected token: (.    What could be the trouble with my code. Pls help me

// SOQL query to get the nearest Contacts        
 String queryString = 'SELECT Id, Name, Location__longitude__s, Location__latitude__s ' +
                             'FROM Contact'+    
                          'WHERE DISTANCE(Location__c, GEOLOCATION('+lat+','+lon+'), \'mi\') < 20 ' +                              'ORDER BY DISTANCE(Location__c, GEOLOCATION('+lat+','+lon+'), \'mi\')' +   
                           'LIMIT 10';      
   // Run the query         
List <Contact> contacts = database.query(queryString);

This is my Debug log , for reference.

User-added image
Best Answer chosen by Reshmi Smiju
bob_buzzardbob_buzzard
It looks to me like you are missing some spaces - in these two lines:
 
'FROM Contact'+    
'WHERE DISTANCE(Location__c, GEOLOCATION('+lat+','+lon+'), \'mi\') < 20 ' +

there will be no space between the Contact and WHERE keywords, plus on these lines:
 
'ORDER BY DISTANCE(Location__c, GEOLOCATION('+lat+','+lon+'), \'mi\')' +   
'LIMIT 10';
there will be no space between the closing bracket and the LIMIT keyword.

I would output the query using 'System.debug' and check the format of it. You could then fix it up in the query editor to find any other problems.

All Answers

bob_buzzardbob_buzzard
It looks to me like you are missing some spaces - in these two lines:
 
'FROM Contact'+    
'WHERE DISTANCE(Location__c, GEOLOCATION('+lat+','+lon+'), \'mi\') < 20 ' +

there will be no space between the Contact and WHERE keywords, plus on these lines:
 
'ORDER BY DISTANCE(Location__c, GEOLOCATION('+lat+','+lon+'), \'mi\')' +   
'LIMIT 10';
there will be no space between the closing bracket and the LIMIT keyword.

I would output the query using 'System.debug' and check the format of it. You could then fix it up in the query editor to find any other problems.
This was selected as the best answer
Reshmi SmijuReshmi Smiju
Thank you so much Bob :) It worked . There was a space problem.
I was trying to do the Developer console. It was working fine, with the values .
Anyway Thanks much Again. :)
Reshmi SmijuReshmi Smiju
Hi Bob,

I have come across some trouble with my code. I need to pass some list of values to VF page from My Class. My code is below.
In this, I am marking some latitude + longitude  on the google Map.  locations is coming from the Apex Class.
Along with location ,  I need to pull contacts details +mailing Address to show them in the small window in map. I know, the expression  !contacts.Name , is wrong. How to correct my code. Pls advise.

PAGE CODE
<apex:pageBlockSection rendered="{!resultsAvailable}" title="Locations">
            <apex:map width="600px" height="400px">
                <apex:repeat value="{!locations}" var="pos">
                    <apex:mapMarker position="{!pos}">
                        <!-- Add info window with contact details -->
                        <apex:mapInfoWindow >
                        <apex:outputPanel layout="block" style="font-weight: bold;">
                            <apex:outputText>{!contacts.Name }</apex:outputText>
                        </apex:outputPanel>
                      </apex:mapInfoWindow>
                    </apex:mapMarker>
                </apex:repeat>

            </apex:map>
        </apex:pageBlockSection>

APEX CLASS CODE

 String queryString =            'SELECT Id, Name, MailingStreet, MailingCity, MailingState, Phone, Location__longitude__s, Location__latitude__s ' +            'FROM Contact ' +            'WHERE DISTANCE(Location__c, GEOLOCATION('+lat+','+lon+'), \'mi\') < 20 ' +            'ORDER BY DISTANCE(Location__c, GEOLOCATION('+lat+','+lon+'), \'mi\') ' +            'LIMIT 10';         System.Debug('>>>> the value of queryString is ' + queryString);
        // Run the query         
List <Contact> contacts = database.query(queryString);         
if(contacts.size() > 0 ) {             
// Convert to locations that can be mapped             
locations = new List<Map<String,Double>>();       
      for (Contact con : contacts) {                 
locations.add( new Map<String,Double>{                         
'latitude' => con.Location__latitude__s,                          
'longitude' => con.Location__longitude__s                     
}                 
);             
}         
}         
else {             
System.debug('No results. Query: ' + queryString);         
}

Thanks much in Advance.
Reshmi