Skip to main content
Mark Cuban asked in #Apex
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?

5 answers
  1. Jan 19, 2015, 5:22 AM
    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.
0/9000