Monday, January 4, 2016

System.QueryException: unexpected token: 'where'

Whenever you’re making any dynamic query construction in apex we will be ending with many errors .Among them one common error is System.QueryException: unexpected token: 'where' 

Code Snippet:
 String queryString = 'Select id,name,industry,';  
 queryString = queryString +'From Account';  
 System.debug('queryString ..'+queryString);  
 Database.query(queryString);  

When I run above code snippet I'm receiving System.QueryException: unexpected token: 'where' and then I checked my debug for the query it's something like as below

Debug value : Select id,name,industry, From Account

WorkAround 

 The extra comma in between Industry and From is causing the issue so if you remove that extra comma it will work perfectly.

Modified Code Snippet:
 String queryString = 'Select id,name,industry ';  
 queryString = queryString +'From Account';  
 System.debug('queryString ..'+queryString);  
 Database.query(queryString);  

So when ever your making dynamic quires please keep below points in mind

 1. Any extra commas are coming in queries
 2. Whether space is provided between last column and FROM statement
 3. Give space between From and Object Name

Got the solution....Enjoy..........!

No comments:

Post a Comment