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
rapcorrearapcorrea 

Error IN operator must be used with an iterable expression when passing list of strings as query parameter

Hi there,

Can anybody help me with this code?
I'm trying to generate a list of string values to use as input parameter for another query but when I run it, I keep getting the error System.QueryException: IN operator must be used with an iterable expression.
Refer to the code below:
global class BatchUpdateProductsStagingStatus implements Database.Batchable<sObject>, Database.Stateful {
    
    global Integer varCountParent = 0;
	global Integer varCount = 1;
    global String listParam = '';
    global String listParamfinal ;
    global String query;
	global String queryCondition;
   
    global Database.QueryLocator start(Database.BatchableContext scope) {
        
        List<AggregateResult> ListCatalogItens = new List<AggregateResult>();
        List<AggregateResult> referenceList = [select Parent_Catalog_Item__r.External_Id__c a from Catalog_Item__c where RecordType.Name = 'Format' and Parent_Catalog_Item__r.External_Id__c != null group by Parent_Catalog_Item__r.Id ,Parent_Catalog_Item__r.External_Id__c , Reference_Catalog__c HAVING Count(Id) >1 ORDER BY Parent_Catalog_Item__r.External_Id__c];        
               
        for (AggregateResult c: referenceList) {

            listParam = string.valueOf(c).removeStart('AggregateResult:').removeStart('{a=').removeEnd('}');


            if ( referenceList.size() > 1){				
				if (varCount < referenceList.size()){
					
					listParam = '\''+ String.escapeSingleQuotes(listParam) + '\', ';					
					listParamfinal = listParamfinal + listParam;

					
				}else if (varCount == referenceList.size()){
					listParam = '\''+ String.escapeSingleQuotes(listParam) + '\' ';
					listParamfinal = listParamfinal + listParam;

					}
            }else{
				listParam = '\''+ String.escapeSingleQuotes(listParam) + '\'';
				listParamfinal = listParamfinal + listParam;


			}

			varCount = varCount + 1;
			System.debug(referenceList.size());
            
        }
		
		if(varCount > 1){
			listParamfinal = '('+listParamfinal+')';
			queryCondition = 'and Code__c IN: listParamfinal';
			
		}else queryCondition = 'and Code__c =: listParamfinal';
		
        System.debug('Qtde de registros: '+varCount);
        System.debug(listParamfinal ); 
		System.debug(queryCondition);
               

        query = 'select Id, StatusMessage__c from Products__c where StatusProcess__c = \'Processed\' '+queryCondition;
        
         System.debug(query); 
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext scope, List<Products__c> lstProducts ){
        
        List<Products__c> Prod2Update = new List<Products__c>();
        
        for (Products__c varProd2Update : lstProducts) {
            
            varProd2Update.StatusMessage__c = 'The staging record was created on Catalog Item as a Stem but it does not have any associated Leaf record with attribute Reference Catalog = TRUE';
            Prod2Update.add(varProd2Update);
            varCountParent = varCountParent + 1;
                                      
        }
        
        update Prod2Update;
            
    }    
    global void finish(Database.BatchableContext scope ){
        System.debug(varCountParent + ' records processed.');
    }    
}
Thanks in advance
 
Best Answer chosen by rapcorrea
Sunad RasaneSunad Rasane
Hi Raphael,

From the code above I can say that you have used 'listParamfinal' variable of type string for querying.
Instead, use List<String> listParamfinal and add the values in it before querying. 

I hope it helps. Please get back if you still are unable to solve it.

All Answers

Sunad RasaneSunad Rasane
Hi Raphael,

From the code above I can say that you have used 'listParamfinal' variable of type string for querying.
Instead, use List<String> listParamfinal and add the values in it before querying. 

I hope it helps. Please get back if you still are unable to solve it.
This was selected as the best answer
rapcorrearapcorrea
Hi Sunad,

Thanks for your help. I did exactly what you suggested and it worked.

Thanks a lot!