+ Start a Discussion
Lakshmi SLakshmi S 

write a batch class for to delete all contacts of accounts where account field: active__c="yes"

Hi Team,


Write a batch class for to delete all contacts of accounts where Account field : active__c="yes" ?


Please give me the reply to above scenario....

Regards
Lakshmi
Best Answer chosen by Lakshmi S
Amit Chaudhary 8Amit Chaudhary 8
If you want to delete all contact then try below code
global class DeleteRecords implements Database.Batchable<sObject>{

	global Database.querylocator start(Database.BatchableContext BC)
	{
		string query  = 'Select Id from Contact where Account.active__c = \'yes\'';
		return Database.getQueryLocator(query);
	}

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

	global void finish(Database.BatchableContext BC){
	}
}

If you want to delete all Account then try below code
global class DeleteRecords implements Database.Batchable<sObject>{

	global Database.querylocator start(Database.BatchableContext BC)
	{
		string query  = 'Select Id from Account where active__c = \'yes\'';
		return Database.getQueryLocator(query);
	}

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

	global void finish(Database.BatchableContext BC){
	}
}

Let us know if this will help you
 

All Answers

Waqar Hussain SFWaqar Hussain SF
try below code
 
global class DeleteRecords implements Database.Batchable<sObject>{

global Database.querylocator start(Database.BatchableContext BC){
    string query  = 'Select Id from Contact where Account.active__c = \'yes\'';
    return Database.getQueryLocator(query);
}

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

global void finish(Database.BatchableContext BC){
}
}

 
Lakshmi SLakshmi S
Hi Waqar Hussain,

Thanks for your reply.
I have doubt on this scenario.
Delete all contacts of accounts which are active means - delete all contacts related to accounts or delete all accounts related to accounts which are active.
Amit Chaudhary 8Amit Chaudhary 8
If you want to delete all contact then try below code
global class DeleteRecords implements Database.Batchable<sObject>{

	global Database.querylocator start(Database.BatchableContext BC)
	{
		string query  = 'Select Id from Contact where Account.active__c = \'yes\'';
		return Database.getQueryLocator(query);
	}

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

	global void finish(Database.BatchableContext BC){
	}
}

If you want to delete all Account then try below code
global class DeleteRecords implements Database.Batchable<sObject>{

	global Database.querylocator start(Database.BatchableContext BC)
	{
		string query  = 'Select Id from Account where active__c = \'yes\'';
		return Database.getQueryLocator(query);
	}

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

	global void finish(Database.BatchableContext BC){
	}
}

Let us know if this will help you
 
This was selected as the best answer
Lakshmi SLakshmi S
HI Amit,

Thanks for your reply.
My doubt is -- Delete all Contacts of Accounts.
query for delete all contacts of accounts.
Amit Chaudhary 8Amit Chaudhary 8
Hi Lakshmi,

Yes in above example first Batch is deleteing all contacts of account where active ='Yes'.

Let us know if you need more detail
Lakshmi SLakshmi S
Hi Amit,

Thanks for your reply.