List<id> at IN clause in Dynamic SOQl

  • I am fetching all the field from sObject and querying using Database.query(). When I query with List of id's it is throwing Invalid ID ('id', 'id', ...) error.

    for example at static soql we can query with list of id's like below

    select id, name from Account where id in:ids

    but am getting error when i use in Dynamic SOQL.

    Am using below code to fetch and query.

        List<id> ids = new List<id>();
        List<Patient__c> paList;
            String query;
            String SobjectApiName = 'Patient__c';
            Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
            Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
            String commaSepratedFields = '';
            for(String fieldName : fieldMap.keyset()){
                if(commaSepratedFields == null || commaSepratedFields == ''){
                    commaSepratedFields = fieldName;
                    commaSepratedFields = commaSepratedFields + ', ' + fieldName;
            /////here i want to query with set of id's///
            query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName +' where id='+'\'' + ids + '\'';
           ////error occuring here as INVALID ID ('id','id', .... )////
            paList = Database.query(query);

    Problem 1 :How to query the list of id's in dynamic query?

    Problem 2 :I have a Look Up to Account object(Account is Master). When i query like above it is throwing as

    "retrieved soql without querying the requested field:Patient__c.Account__r".

    How to Query the lookup relation in Dynamic query.


    You should use your list name in the same manner in your dynamic query: `where id=:ids;`

  • Ratan Paul

    Ratan Paul Correct answer

    6 years ago

    Simple Just use set inside single quote

    query = 'select ' + commaSepratedFields + ' , from ' + SobjectApiName +' where id IN: ids ';


    Add Account__r.Name field in query

    query = 'select Account__r.Name, ' + commaSepratedFields + ' , from ' + SobjectApiName +' where id IN: ids ';

    Thanks. Simple but forgot. Thanks a lot. I have another error. I have a lookup to Account in Patient__c object. When I use dynamic query to fetch all the field from Patient__c it is throwing "retrieved SOQL without querying the requested field: Patient__c.Account__r ". How write a query for this?. I'll also update my question for the same.

    @mohammedazarudeen check my edit

    @mohammedazarudeen it should be like `Account__r.Name` or other field

    When i add Patient__c.Account__r it is showing No such column in Patient__c. Api Name of the lookup field is Account__c. When I check debug logs without adding Patient__c.Account__r it is retrieving Account__c field too. But when i executing without adding Patient__c.Account__r it is throwing No such column. If I add Patient__c.Account__c in query it is showing Duplicate field is not allowed. Any solution?

    Yep Account__r.Name is the solution. Thanks a lot!!Happy smile

    @mohammedazarudeen Yes correct you need to check which all the field you want access from master Account example Name field the add like `Account__r.Name` if there is other fields then add like that only `Account__r.FieldName`

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM