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
SFDC 2017SFDC 2017 

How to pass multiple string value into a constructor of a batch apex at the run time ?

Hi All,

I have written a batch apex in a such a way that if string field we are passing then in query it should take other wise no.It is working for single string value but i need to make it work for multiple string values.

Constructor:
global StateBatch(String strfield,Date startDate, Date enddate) {
        
        if(StartDate!= null && Enddate!= null){
            query = 'select Id,Status_vod__c from State__c where Start_Date__c >=:startDate AND End_Date__c<=:endate';
            if(String.isNotBlank(strfield)){
               query += ' AND State__c IN (:strfield)' ;
            }
        }
    } 

Running in DEv console as 
for multiple markets you run it as 
StateBatch sb = new Statebatch ('\'United States of America\',\'United Kingdom\'',Date.newInstance(2019, 02, 13),Date.newInstance(2019, 02, 14)); 
database.executeBatch(sb);
Multiple values it is not taking .How will change to support multiple values.

Thanks in Advance
Raj VakatiRaj Vakati
The change it as below 
Change your constructor to accept the list of Strings 
 
global StateBatch(List<String> strfieldList,Date startDate, Date enddate) {
        
        if(StartDate!= null && Enddate!= null){
            query = 'select Id,Status_vod__c from State__c where Start_Date__c >=:startDate AND End_Date__c<=:endate';
            if(strfield.size()>0){
               query += ' AND State__c IN (:strfieldList)' ;
            }
        }
    }


The Execute the batch as below 
 
List<String> strList = new List<String>() ; 
	strList.add('United States of America');
	strList.add('United Kingdom');
	
	
	StateBatch sb = new Statebatch (strList,Date.newInstance(2019, 02, 13),Date.newInstance(2019, 02, 14));

 
SFDC 2017SFDC 2017
Thank you Raj.But it is not taking the list values it is always going to the first if block if(StartDate!= null && Enddate!= null){
        query = 'select Id,Status_vod__c from State__c where Start_Date__c >=:startDate AND End_Date__c<=:endate';
Raj VakatiRaj Vakati
Change it as below
 
global StateBatch(List<String> strfieldList,Date startDate, Date enddate) {
        
        if(StartDate!= null && Enddate!= null){
            query = 'select Id,Status_vod__c from State__c where Start_Date__c >=:startDate AND End_Date__c<=:endate';
            if(strfieldList.size()>0){
               query += ' AND State__c IN (:strfieldList)' ;
            }
        }
    }

 
SFDC 2017SFDC 2017
@Raj Vakati Thanks for your suggestion.