+ Start a Discussion
Salesforce Test 5Salesforce Test 5 

Automatic Delete using Custom Bottom

Hi All
I have more than 10000 record in my custom object ,so how to delete all record automatically uisng custom bottom


You can perform this action through apex code.

As per my understanding you want a button and click on that button you want to delete 10000 record.

DML can perform with 10000 thousand reocrd not larger than this count. If you have more than 10000 thousand reocrd you can do this via Dataloader or you can wirte a batch call to delete the records.

Ashlekh Gera
Salesforce Test 5Salesforce Test 5
Hi Ashlesh Have you sample code for this requirement so that i can easily catch
Krishna SambarajuKrishna Sambaraju
Here is a batch class that will delete the records in batches and runs in the background.
global class DeleteRecords implements Database.Batchable<sObject>{
	global Database.QueryLocator start(Database.BatchableContext BC){
		return Database.getQueryLocator('select Id from your_object'); //change to your object name 

	global void execute(Database.BatchableContext BC, List<Your_Object> scope){	                                   
	    delete scope;

	global void finish(Database.BatchableContext BC){
You need to create a webservice to call it from the javascript on the button.
global class DeleteRecordsSvc{

	WebService static void DeleteAllRecords()
		Database.executeBatch(new DeleteRecords());
Javascript on the button to call the webservice method.

sforce.apex.execute("DeleteRecordsSvc", "DeleteAllRecords");

Hope this helps.
Salesforce Test 5Salesforce Test 5
Hi Krishna I have tried this ,after creation of custom bottom when i click to delete its showing error like.. A problem with the OnClick JavaScript for this button or link was encountered: args not specified
Krishna SambarajuKrishna Sambaraju
Pass empty arugument list as follows.
sforce.apex.execute("DeleteRecordsSvc", "DeleteAllRecords", { });
Hope this works.
Salesforce Test 5Salesforce Test 5
Hi Krishna How to write test class for this web service class global class DeleteRecordsSvc{ WebService static void DeleteAllRecords() { Database.executeBatch(new DeleteRecords()); } }
Krishna SambarajuKrishna Sambaraju
Here it is.
public class DeleteRecordsSvcTest{
    static testMethod void TestDeleteAllRecords(){
		List<your_object> records = createRecords();
		records = [select Id from your_object where Id in :records];
		system.assertEquals(0, records.size());
	static List<your_object> createRecords()
		List<your_object> records = new List<your_object>();
		for (i=1; i <= 100; i++)
			your_object record = new your_object(Name = 'Test Record ' + i, field1 = 'Test value ' + i, field2 = 'Test value' + i);//change / add your field names accordingly.
		insert records;
		return records;
Change the object names and field names accordingly. Hope this helps.