function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
OnkiOnki 

Dynamic SOQL is not working as per expectations

Hi,

 

public void groupAdminEngineBatch(Set<Id> AccountId)
     {    
      List<Contact> contactsList = new List<Contact>([Select Id from Contact where AccountId in : AccountId]);
      for(Contact C : contactsList )
      {
       ContactId.add(C.Id);  
      }
     Query= 'Select Id from user where ContactId  in  '+ContactId+'';
    Database.Query(query);
}

This is Error I got in system debug.

09:43:57.047 (47263000)|SYSTEM_METHOD_ENTRY|[27]|System.debug(ANY)
09:43:57.047 (47309000)|USER_DEBUG|[27]|DEBUG|>>> Test >>>>>> Select Id from user where ContactId  in  (0038000000fVQIUAA4, 0038000000fVRloAAG, 0038000000fVQw3AAG, 0038000000fVQw4AAG, 0038000000fVQw5AAG, 0038000000fVQw6AAG, 0038000000fVRlpAAG, 0038000000fVRlqAAG, 0038000000fVRlrAAG, 0038000000fVSNUAA4, ...) ------ this is Last string created by sfdc if colleaction string is more than 255.
09:43:57.047 (47320000)|SYSTEM_METHOD_EXIT|[27]|System.debug(ANY)
09:43:57.047 (47341000)|SYSTEM_METHOD_ENTRY|[28]|Database.getQueryLocator(String)
09:43:57.047 (47527000)|EXCEPTION_THROWN|[28]|System.QueryException: expecting a right parentheses, found 'fVQIUAA4'
09:43:57.047 (47575000)|SYSTEM_METHOD_EXIT|[28]|Database.getQueryLocator(String)
09:43:57.047 (47645000)|FATAL_ERROR|System.QueryException: expecting a right parentheses, found 'fVQIUAA4'

Seems Query is fine but error is unexpected.


I am getting error seems like dynamic Query not support the IN parameter,if Collection of ID is more than 255.

mulvelingmulveling

Dynamic SOQL is working fine here -- by using string concatenation to add the List<Contact>, you're implicitly invoking its toString method, which makes no gurantees of outputting into valid SOQL In-clause format. In fact, it ususally doesn't -- in this case, for one, the Id values need to be single-quoted. 

 

You could easily write a List stringify, via loop, to output the proper format, but if possible I'd just install the apex-lang managed package and use al.SOQLBuilder for constructing dynamic SOQL. It typically results in cleaner code for things like this. 

OnkiOnki

That is work around i was trying.

 

If you use SET<ID> and rather than List<String>  your will get string invalid error from query.

 

Take  example where collection size is more than 200 and use them in dynamic IN clause.

 

 

Regards

Onkar.

 

OnkiOnki

If you use Collection as ID the query builder will give such kind of error.

 

Seems salesforce does not consider ID in Query. System is not considering it ID, Consider as string so I will trying to put string as collection

 

 

06:51:57.042 (42799000)|SYSTEM_METHOD_ENTRY|[39]|Database.getQueryLocator(String)
06:51:57.043 (43016000)|EXCEPTION_THROWN|[39]|System.QueryException: expecting a right parentheses, found 'fVQIUAA4'
06:51:57.043 (43068000)|SYSTEM_METHOD_EXIT|[39]|Database.getQueryLocator(String)
06:51:57.043 (43138000)|FATAL_ERROR|System.QueryException: expecting a right parentheses, found 'fVQIUAA4'
OnkiOnki

  global groupAdminEngineBatch(Set<Id> AccountId, String GroupId)
     {
      Group G = [Select Id from Group where Name =: GroupId limit 1];
      GroupId = G.Id;
      List<String> ContactId = new List<String>();
      
      List<Contact> contactsList = new List<Contact>([Select Id from Contact where AccountId in : AccountId]);
      
      for(Contact C : contactsList )
      {
      ContactId.add('\''+C.Id+'\'');  
       //ContactId.add(C.Id);  
      }
        //Query= 'Select Id from user where ContactId  =  '+ContactId+'';
        Query = 'Select Id from user where ContactId  in  '+ContactId+'';

 

    Database.query(Query);

 

    }

 

If you use String in collection you will get such kind of error.

 

07:01:01.058 (58502000)|SYSTEM_METHOD_ENTRY|[37]|Database.Query(String)
07:01:01.058 (58677000)|EXCEPTION_THROWN|[37]|System.QueryException: unexpected token: '.'
07:01:01.058 (58721000)|SYSTEM_METHOD_EXIT|[37]|Database.Query(String)
07:01:01.058 (58787000)|FATAL_ERROR|System.QueryException: unexpected token: '.'