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
nilesh walkenilesh walke 

Every morning at 7 AM for account we have to create Task record by using Batch class

global class CreateTaskBatch implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext bc)
{
return Database.getQueryLocator([select id,ownerid,name from account]);
}
global void execute(Database.BatchableContext bc,List<Account> acclist)
{
list<task> ta=new list<task>();
for(Account a:acclist)
{
task t=new task();
t.OwnerId=a.OwnerId;
t.WhatId=a.Id;
t.Id=a.Id;
t.Description='Created task for Account';
tas.add(t);
}
insert ta;
}
global void finish(Database.BatchableContext bc)
{

}
}
test class for this
CharuDuttCharuDutt
Hil Nilesh Walke
Try Below Code

Please Comment This In Your Batch Class   " t.Id=a.Id " on line number 14 As ID Is Self Generating Field And Not Editable
@isTest
public class BatchProcessorTest {
  @testSetup
  static void setup() {
		List<Account> lstAcc = new List<Account>();
	for(Integer i=0; i <100; i++) {
		Account acc = new Account();
    		
        	acc.Name='test'+i;
        	
            lstAcc.add(acc);
	}
		insert lstAcc;
	}

	static testmethod void test() {

	CreateTaskBatch lp =new CreateTaskBatch();
		Id BatchId = Database.executeBatch(lp);
	}
}


Please Mark It As Best Answer If It Helps
Thank You!