• #9840
    Salesforce | Nitish Nitish #398
    Forcetalks

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

    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: ‘(‘

     

    #9855
    Salesforce | Suyash Suyash #400
    Forcetalks

    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’ ;

    #9913
    Salesforce | Anup Anup #89
    Forcetalks

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

    #35422
    Salesforce | Parul Parul #2
    Forcetalks

    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 ’ ;

     

Viewing 4 posts - 1 through 4 (of 4 total)

Please to reply to this topic.

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2018 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?