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
Kevin Tullos 7Kevin Tullos 7 

Please help me call a batch Apex Class.

Please help me write the code to execute this batch...
 
global class Delete_Batch implements Database.Batchable<sObject>
{

    String query, value, field;
    
    global Delete_Batch(string s)
    {
        Query=s;
        //query = 'SELECT Id FROM Delete_Batch_Test__c';
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
       return Database.getQueryLocator(query); 
    }
   
    global void execute(Database.BatchableContext BC, List<sObject> scope)
    {
        Delete Scope;
    }
     
    global void finish(Database.BatchableContext BC)
    {
    }
}
I wrote something, but it could not get it to run.  I don't know where to start.  Could you please help me...

 
Best Answer chosen by Kevin Tullos 7
ManojjenaManojjena
Hi Kevin ,
You can execute batch apex by using two static method in Database class

executeBatch(Instance Of class )
excecuteBatch(InstanceOf batch class ,batchsize )
So you can write code below in console and execute .
String str='SELECT id FROM Account ';
Delete_Batch bdt=new Delete_Batch(str);
Database.executeBatch(bdt);
or
Database.executeBatch(bdt,100);

 

All Answers

ManojjenaManojjena
Hi Kevin ,
You can execute batch apex by using two static method in Database class

executeBatch(Instance Of class )
excecuteBatch(InstanceOf batch class ,batchsize )
So you can write code below in console and execute .
String str='SELECT id FROM Account ';
Delete_Batch bdt=new Delete_Batch(str);
Database.executeBatch(bdt);
or
Database.executeBatch(bdt,100);

 
This was selected as the best answer
ManojjenaManojjena
Hi Kevin ,

for other object you need to modify the query .and add your object name inplace of Account .If you need any condition you can add in your query .
Kevin Tullos 7Kevin Tullos 7
Thank you for helping me.  You really bailed me out big time!!
Amit Chaudhary 8Amit Chaudhary 8
Hi Kevin ,

Please try below code:-

String query = 'SELECT Id FROM Delete_Batch_Test__c'; // Please add your query here.
Database.executeBatch( new  Delete_Batch( query ) );

Please exceute above code in developer console. Please let us know if this will helo you.

Thanks
Amit Chaudhary
Kevin Tullos 7Kevin Tullos 7
The code Amit gave me was the actual code that I used, but both of these worked great.

String query = 'SELECT Id FROM <sobject>'; // Please add your query here.
Database.executeBatch( new  Delete_Batch( query ) );