3

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>();
    ids.add(...);
    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;
            }else{
                commaSepratedFields = commaSepratedFields + ', ' + fieldName;
            }
        }

        /////here i want to query with set of id's///

        query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName +' where id='+'\'' + ids + '\'';
        system.debug('=====query===='+query);

       ////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.

Thanks

  • 2
    You should use your list name in the same manner in your dynamic query: where id=:ids; – Sergej Utko Jan 7 '16 at 10:17
4

Simple Just use set inside single quote

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

Edit

Add Account__r.Name field in query

query = 'select Account__r.Name, ' + commaSepratedFields + ' , from ' + SobjectApiName +' where id IN: ids ';
| improve this answer | |
  • 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. – mohammed azarudeen Jan 7 '16 at 10:26
  • @mohammedazarudeen check my edit – Ratan Paul Jan 7 '16 at 10:34
  • @mohammedazarudeen it should be like Account__r.Name or other field – Ratan Paul Jan 7 '16 at 10:42
  • 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? – mohammed azarudeen Jan 7 '16 at 10:44
  • 1
    @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 – Ratan Paul Jan 7 '16 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.