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
Abhishek RayAbhishek Ray 

Hi need help in writing apex batch test class

Here is the batch apex I wrote to populate owner name for Task into a field. I am not sure how to write a batch apex for this code:


 global class updateTask implements Database.Batchable <SObject>


global final String Query; String email;

global updateTask(String q) {

Query=q; }   

global Database.QueryLocator start(Database.BatchableContext BC)  {   

  return Database.getQueryLocator(query);   


global void execute(Database.BatchableContext BC, List<Task> scope)


List<Task> myTask = new list<Task>();

     for (Task s : scope)         


 Task a = (Task)s;                

  for(Integer i = 0; i < 200; i++)                                              

  {                                        a.Current_Owner__c= [select name from User where ID = : a.OwnerId limit 1].name;                                              }                         myTask.add(s);                            

} update myTask; }   


 global void finish(Database.BatchableContext BC) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

mail.setToAddresses(new String[] {email}); mail.setReplyTo(''); mail.setSenderDisplayName('Batch Processing'); mail.setSubject('Batch Process Completed'); mail.setPlainTextBody('Batch Process has completed');

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });



Please help......






You just need to pass a query to your Batch class constructor.


Refer to this code:


Task t = new Task();

// give all required fields

insert t;


updateTask classObj = new updateTask('Select Id, Current_Owner__c From Task Limit 200 ');