Activity Forums Salesforce® Discussions Getting Error when using map in Dynamic query:- System.QueryException: unexpected token: '(' in the SOQL query

  • Getting Error when using map in Dynamic query:- System.QueryException: unexpected token: '(' in the SOQL query

    Posted by Nitish on April 14, 2016 at 7:40 am

    Map<Id, Contact> mapOfIdVsContact = new Map<Id, Contact>();
    for(Contact con : [Select Id from Contact])
    {
    mapOfIdVsContact.put(con.Id, con);
    }

    String s = 'Select Id from Contact Where Id IN : maps.keySet()' ;

    List<Contact> cont = Database.query(s);
    System.debug('@@' + cont);

    Error:- Line: 37, Column: 1 System.QueryException: unexpected token: '('

    Parul replied 5 years, 6 months ago 4 Members · 3 Replies
  • 3 Replies
  • Suyash

    Member
    April 14, 2016 at 7:50 am

    Hi Nitish

    You can't use Map.keySet() directly in the dynamic, for this you must add map.keySet() either in a set or in list after that you can use it in your Dynamic query.

    In your case what you can do is:

    make a set of ids

    set<id> setofids=new set<id>();

    setofids= maps.keySet();

    now use this set in your dynamic soql like this

    String s = ‘Select Id from Contact Where Id IN :setofids’ ;

  • Anup

    Member
    April 14, 2016 at 8:12 am

    Also I have notices a difference of name between the variable you specified and the one referred in code. That too could be troublesome.

  • Parul

    Member
    September 19, 2018 at 11:46 pm

    Hi,

    You must add map.keySet() either in a set or in list after that you can use it in your Dynamic query.

    set<id> idSet=new set<id>();

    idSet = maps.keySet();

    String s = ‘Select Id, name from Contact where Id IN :idSet ’ ;

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos