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
James George 717James George 717 

Help needed on Dynamic List variable in Database.QueryLocator batch process

Hi Friends,
Help me why I'm not able to pass List in the below code, it compiles but always returns none and the execute method never executes if the value is List.

 
public Database.QueryLocator start(Database.BatchableContext bc) {
        List<String> temp = new List<String>();
        temp.add('James');
        temp.add('George');
        String query = 'SELECT Id, FirstName, LastName ' +
        ' FROM Contact ' +
        ' WHERE LastName =\'' + temp + '\'' ;
        return Database.getQueryLocator(query);
    }
    
    public void execute(Database.BatchableContext bc, List<Contact> scope){
        // process each batch of records
        System.debug('Executing execute---->');
    }

Log shows this 

|SOQL_EXECUTE_BEGIN|[43]|Aggregations:0|SELECT Id, FirstName, LastName  FROM Contact  WHERE LastName ='(James, George)'

Appreciating any help.

Thanks,
James

Best Answer chosen by James George 717
AbhinavAbhinav (Salesforce Developers) 
Hi James ,

I think your query is not framing correct

Try replacing your query with
String query = 'SELECT Id, FirstName, LastName ' +
        ' FROM Contact ' +
        ' WHERE LastName IN: temp' ;
Code
public Database.QueryLocator start(Database.BatchableContext bc) {
        List<String> temp = new List<String>();
        temp.add('James');
        temp.add('George');
        String query = 'SELECT Id, FirstName, LastName ' + ' FROM Contact ' + ' WHERE LastName IN: temp' ;
        return Database.getQueryLocator(query);
    }
    
    public void execute(Database.BatchableContext bc, List<Contact> scope){
        // process each batch of records
        System.debug('Executing execute---->');
    }


If it hepls ,Please mark it as best answer!

Thanks!

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi James ,

I think your query is not framing correct

Try replacing your query with
String query = 'SELECT Id, FirstName, LastName ' +
        ' FROM Contact ' +
        ' WHERE LastName IN: temp' ;
Code
public Database.QueryLocator start(Database.BatchableContext bc) {
        List<String> temp = new List<String>();
        temp.add('James');
        temp.add('George');
        String query = 'SELECT Id, FirstName, LastName ' + ' FROM Contact ' + ' WHERE LastName IN: temp' ;
        return Database.getQueryLocator(query);
    }
    
    public void execute(Database.BatchableContext bc, List<Contact> scope){
        // process each batch of records
        System.debug('Executing execute---->');
    }


If it hepls ,Please mark it as best answer!

Thanks!
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47
Hi James,

You can take reference from the below code and you can use IN operator instead of that.
Public Database.QueryLocator start(Database.BatchableContext bc) {
    List<String> temp = new List<String>();
    temp.add('James');
    temp.add('George');
    String query = 'SELECT Id, FirstName, LastName  FROM Contact WHERE LastName IN: temp' ;
    return Database.getQueryLocator(query);
}

Public void execute(Database.BatchableContext bc, List<Contact> scope){
    // process each batch of records
    System.debug('Executing execute---->'+scope);
}
Public void finish(Database.BatchableContext BC){
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
CharuDuttCharuDutt
Hii James George
Try Below Code
Public Database.QueryLocator start(Database.BatchableContext bc) {
    List<String> lstName= new List<String>();
    lstName.add('James');
    lstName.add('George');
    String query = 'SELECT Id, FirstName, LastName  FROM Contact WHERE LastName IN: lstName' ;
    return Database.getQueryLocator(query);
}

Public void execute(Database.BatchableContext bc, List<Contact> scope){
    /*Process Records*/
    System.debug('list Of Records Got From Query---->'+scope);
}
Public void finish(Database.BatchableContext BC){
}
Please Mark It As Best Answer If It Helps
Thank You!

 
James George 717James George 717
Thanks Abhinav,Suraj and Charu.
All of your responses are correct, since Abhinav responsded first I marked his answer as best.

Thanks again,
James