Mass Delete Records using Apex (Any Object) #inSalesforce
global class massBatchDelete Implements Database.Batchable<sObject>{
String theObjectName;
global massBatchDelete(String theObjectName) {
this.theObjectName = theObjectName;
}
global Database.queryLocator start(Database.BatchableContext bc) {
String SOQL = 'SELECT Id FROM ' + theObjectName;
return Database.getQueryLocator(SOQL);
}
global void execute(Database.BatchableContext bc, List <sObject> listedRecords) {
delete listedRecords;
}
global void finish(Database.BatchableContext bc) {
}
}
global massBatchDelete(String theObjectName) {
this.theObjectName = theObjectName;
}
global Database.queryLocator start(Database.BatchableContext bc) {
String SOQL = 'SELECT Id FROM ' + theObjectName;
return Database.getQueryLocator(SOQL);
}
global void execute(Database.BatchableContext bc, List <sObject> listedRecords) {
delete listedRecords;
}
global void finish(Database.BatchableContext bc) {
}
}
//In Execute Anonymous Window of Developer Console Try:
/*
massBatchDelete mdr = new massBatchDelete('Candidate__c');
Database.executeBatch(mdr);
*/
massBatchDelete mdr = new massBatchDelete('Candidate__c');
Database.executeBatch(mdr);
*/
Related Test Class:
@isTest
private class massBatchDeleteTest {
static testMethod void testMassBatchDelete() {
Candidate__c c = new Candidate__c(Name = 'Sample');
insert c;
Test.startTest();
massBatchDelete mdr = new massBatchDelete('Candidate__c');
Database.executeBatch(mdr);
Test.stopTest();
}
}
private class massBatchDeleteTest {
static testMethod void testMassBatchDelete() {
Candidate__c c = new Candidate__c(Name = 'Sample');
insert c;
Test.startTest();
massBatchDelete mdr = new massBatchDelete('Candidate__c');
Database.executeBatch(mdr);
Test.stopTest();
}
}
Comments
Post a Comment