Activity Forums Salesforce® Discussions How to use IN clause in Dynamic query?

  • Amarkant

    Member
    July 21, 2017 at 10:04 am

    MAP<ID,Opportunity> os = new MAP<Id,Opportunity>([Select ID from Opportunity]);
    SET<ID> keys = os.keyset();
    String s = 'SELECT RecordTypeId, COUNT(Id) os FROM Opportunity Where Id IN ';
    s+=':keys GROUP BY RecordTypeId';
    AggregateResult[] ars = Database.query(s);
    System.debug(ars[0].get('RecordTypeId'));

  • Vivek

    Member
    July 23, 2017 at 10:02 am

    It is very simple assume you have a Set<String> or Set<Id> named "ids"

    Set ids = new Set();

    String dynamicQuery = "SELECT ID FROM SomeObject__c WHERE SomeID IN :ids";

    Database.query(dynamicQuery);

    You don't have to do anything when complier will compile the code it will find set of ids and put it into your query

  • Parul

    Member
    September 16, 2018 at 4:47 am

    Hi

    list<string> setAccIds = new list<string>();
    setAccIds .addAll(AcctoCon_Sync.keySet());

    AccQuery = 'SELECT Id ,AccArticleId , ' + title + ',' + answer + ' FROM ' + articleType;
    AccQuery += ' WHERE PublishStatus = \'Online\' AND language = \'en_US\' AND AccQuery IN :' + setAccIds ;

     

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos