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
Jefferson  EscobarJefferson Escobar 

Where should I define the query in Apex batch, in the scheduled class or in the apex batch class?

Hi,
I have a apex batch that is execueted from Schedulre class. Curently I am passing the query from the schduled class in order to limit the records in the test class but I am not sure where is the best place to declare the query, what would be the best practie  ? or Where should I declare the query, in the schedulable class or directly in the apex batch adding a filter as global variable ?

Thanks for you help,
Jeff
Best Answer chosen by Jefferson Escobar
Amit Chaudhary 8Amit Chaudhary 8
Both are good as per salesforce best pratice. But if you will define the query inside the scheduler class like Example 2. Then you can pass the dynamic query according to requirement. 

So you can try exmple 2.
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi Jefferson,

You can define the query in two places
1) One in Batch job start method.
2) Or you can define the query in schedule also also,

Exampe 1:- Query inside batch job
global class ExampleBatchClass implements Database.Batchable<sObject>
{

        global Database.QueryLocator start(Database.BatchableContext BC)
		{
			String query ='select name from account';
			return Database.getQueryLocator(query);
        }
      
      // Execute Logic
       global void execute(Database.BatchableContext BC, List<sObject>scope)
	   {
              // Logic to be Executed batch wise      
     
       }
     
       global void finish(Database.BatchableContext BC)
	   {
            // Logic to be Executed at finish
       }
    }

Example 2:- 
global class SearchAndReplace implements Database.Batchable<sObject>
{

	global final String Query;
   
	global SearchAndReplace(String q )
	{
		Query=q; 
	}

	global Database.QueryLocator start(Database.BatchableContext BC)
	{
		return Database.getQueryLocator(query);
	}

	global void execute(Database.BatchableContext BC, List<sObject> scope)
	{
		for(sobject s : scope)
		{
			s.put(Field,Value); 
		}
		update scope;
	}

	global void finish(Database.BatchableContext BC)
	{

	}
}

To execute above batch job you need to pass query inside scheduler class or in developer console.
String q = 'SELECT Industry FROM Account LIMIT 10';
Id batchInstanceId = Database.executeBatch(new SearchAndReplace(q), 5);

Please check below post for  more detail:-
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm


Please mark this as solution if this will help you. So that if some one has same issue this post can help others.

Thanks
Amit Chaudhary
Amit Chaudhary 8Amit Chaudhary 8
Both are good as per salesforce best pratice. But if you will define the query inside the scheduler class like Example 2. Then you can pass the dynamic query according to requirement. 

So you can try exmple 2.
 
This was selected as the best answer
Jefferson  EscobarJefferson Escobar
Thanks Amit, I've been working through different implementations without getting a standard way, I'd say we can declare the query depending on the business needs.