You need to sign in to do that
Don't have an account?
Sriyesh 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);
}
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);
}
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
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.
Hi,
Please find the solution.
Please mark it as the Best Answer if it helps you.
Thank You
This Might be a correct solution but wouldn't this affect the SOQL 1001 row exception?
Thanks
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
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
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);
}