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
deter danglerdeter dangler 

Database.query() method throwing syntax error

I have the below code:
 
Set<String> fieldNames = schema.describeSObjects(new String[] {'User'})[0].fields.getMap().keyset();
List<String> iterableFields = new List<String>(fieldNames);
String query = 'SELECT '+ String.join(iterableFields,',')+ ' FROM User WHERE Id=:UserInfo.getUserId()';
User user = Database.query(query); //Line 10
When running the code, I am getting the below error:
  1. line 10, column 1
  2. System.QueryException: unexpected token: '('
Can somebody help what is the problem in this?
Best Answer chosen by deter dangler
MithunPMithunP
Hi deter dangler,

Instead of using UserInfo.getUserId() directly in query string, take a ID variable and use that id value in query string. 

Here is the updated code try this.
Set<String> fieldNames = schema.describeSObjects(new String[] {'User'})[0].fields.getMap().keyset();
List<String> iterableFields = new List<String>(fieldNames);
Id usrId = UserInfo.getUserId();

String query = 'SELECT '+ String.join(iterableFields,',')+ ' FROM User WHERE Id=: usrId';
User user = Database.query(query); //Line 10
Best Regards,
Mithun.

All Answers

James LoghryJames Loghry
Interesting.  Apparently it doesn't like your "UserInfo.getUserId()" binding.  Try Id=\'' + UserInfo.getUserId() + '\'; instead.
James LoghryJames Loghry
Sorry, that should have been Id=\'' + UserInfo.getUserId() + '\'';
deter danglerdeter dangler
Hi James,

Just learnt from this link http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_soql.htm

that we cannot use "dynamic SOQL can’t use bind variable fields in the query string"

so i just changed my code to
 
Set<String> fieldNames = schema.describeSObjects(new String[] {'User'})[0].fields.getMap().keyset();
List<String> iterableFields = new List<String>(fieldNames);
String userId = UserInfo.getUserId(); //This line i added
String query = 'SELECT '+ String.join(iterableFields,',')+ ' FROM User WHERE Id=:userId';
User user = Database.query(query); //Line 10

and everything worked fine


 
MithunPMithunP
Hi deter dangler,

Instead of using UserInfo.getUserId() directly in query string, take a ID variable and use that id value in query string. 

Here is the updated code try this.
Set<String> fieldNames = schema.describeSObjects(new String[] {'User'})[0].fields.getMap().keyset();
List<String> iterableFields = new List<String>(fieldNames);
Id usrId = UserInfo.getUserId();

String query = 'SELECT '+ String.join(iterableFields,',')+ ' FROM User WHERE Id=: usrId';
User user = Database.query(query); //Line 10
Best Regards,
Mithun.
This was selected as the best answer
James LoghryJames Loghry
You are in fact using variable binding here, FYI.  I suspect it has to do with calling a method (for instance getUserId in this case) that resolved into the variable binding, however.