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
Sriyesh M 18Sriyesh M 18 

Can we pass List of records in a apex batch class query ?

Hi All,
 
I am trying to pass a list of records into a batch class using constructor. And adding that to the query. But I am getting the following error :
First error: Only variable references are allowed in dynamic SOQL/SOSL. Can someone help.
 
Code :- 
String query = 'SELECT Id, Name, ProductCode FROM Product2 WHERE IsActive =true';
public ConstructorName(List<Product2> productlst){
        if (!productlst.isEmpty() && productlst!=null) {
            query += ' AND APTS_EXT_ID__c IN : '+productlst; 
        }
    }
global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(query);
    }
Best Answer chosen by Sriyesh M 18
Sriyesh M 18Sriyesh M 18
Hi Got it.
Thanks Suraj and Hara.

We have to add the list variable as a string and give it as a public list variable. Then it will work in the start method.

Code :
String query = 'SELECT Id, Name, ProductCode FROM Product2 WHERE IsActive =true';
Public List<String> productNewList = new List<String>();
public ConstructorName(List<Product2> productlst){
        for(Product2 pro:productlst){
               productNewList.add(pro.APTS_EXT_ID__c);
        }
        if (!productlst.isEmpty() && productlst!=null) {
            query += ' AND APTS_EXT_ID__c IN : productNewList' ;
        }
    }
global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(query);
    }

All Answers

Hara SahooHara Sahoo
 if (!productlst.isEmpty() && productlst!=null) {
            query += ' AND APTS_EXT_ID__c IN ' + \''+productlst+'\' ; 
        }

Please try this workaround: Remove bind variable from the query string and add single quotes before and after.
This is due to the (':') bind variable usage in dynamic query construction.
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please find the solution.

public List<Product2> productlstInside{set;get;}
 public ConstructorName(List<Product2> productlst){
        if (!productlst.isEmpty() && productlst!=null) {
            query += ' AND APTS_EXT_ID__c IN : '+productlst; 
        }
		
		set<Id> aptSet=new set<Id>();
		for(Product2 pd:productlst){
		aptSet.add(pd.APTS_EXT_ID__c);
		}
		productlstInside=[SELECT Id, Name, ProductCode FROM Product2 WHERE IsActive =true and APTS_EXT_ID__c in: aptSet];
    }
	
	global List<Product2>  start(Database.BatchableContext bc) {
        return productlstInside;
    }

Please mark it as the Best Answer if it helps you.

Thank You

Sriyesh M 18Sriyesh M 18
Hi Suraj,

This Might be a correct solution but wouldn't this affect the SOQL 1001 row exception?

Thanks
Suraj Tripathi 47Suraj Tripathi 47

yeah, it would affect.

But if you are not using so many queries in your code then it would be fine.

and you are also using   return Database.getQueryLocator(query); i.e. this line is similar to my code.

If your code is working fine please mark my answer as Best.

Thank You

 

 

Sriyesh M 18Sriyesh M 18
Hi Suraj,

I am getting this error because of the query, this was the reason i was going for batch job
System.LimitException: Too many query rows: 50001
Sriyesh M 18Sriyesh M 18
Hi Got it.
Thanks Suraj and Hara.

We have to add the list variable as a string and give it as a public list variable. Then it will work in the start method.

Code :
String query = 'SELECT Id, Name, ProductCode FROM Product2 WHERE IsActive =true';
Public List<String> productNewList = new List<String>();
public ConstructorName(List<Product2> productlst){
        for(Product2 pro:productlst){
               productNewList.add(pro.APTS_EXT_ID__c);
        }
        if (!productlst.isEmpty() && productlst!=null) {
            query += ' AND APTS_EXT_ID__c IN : productNewList' ;
        }
    }
global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(query);
    }
This was selected as the best answer