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
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.
 
Lakshmi SLakshmi S
Hi Amit,

How to write test class for rollup summary trigger (100% code coverage)?
Trigger:
trigger FinancialsCountTrigger on Financials__c (after insert, after update, after delete, after undelete) {
    
    if(checkRecursive.runOnce()){
        
        List<Account> updateAcc = new List<Account>();
        Set<Id> accid = new Set<Id>();
        
        Decimal totalRevenue;
        Decimal totalEBITDA;
        
        if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete)){
            for(Financials__c fin : Trigger.New){
                if(fin.AccountName__c != null){
                    accid.add(fin.AccountName__c);
                }
            }
        }
        
        if(Trigger.isAfter && (Trigger.isUpdate || Trigger.isDelete)){
            for(Financials__c fin : Trigger.Old){
                if(fin.AccountName__c != null){
                    accid.add(fin.AccountName__c);
                    //system.debug('-----'+accid);
                }
            }
        }
        
            if(accid.size() > 0){
            List<Account> acclist = [select id,Name,Global_Revenue_Total__c,Global_EBITDA_B4_Total__c,
                                     (select id,Global_Revenue_Total__c,Global_EBITDA_B4_Total__c from Financial__r) from Account where id in :accid];
            
            for(Account acc : acclist){
                
                
                List<AggregateResult> aggres = [Select sum(Global_Revenue_Total__c)revtot,sum(Global_EBITDA_B4_Total__c)ebitdatot
                                                from Financials__c where AccountName__c=:acc.id and Year__c='FY-2017'];
                for(AggregateResult ag : aggres){
                    totalRevenue = (Decimal)ag.get('revtot');
                    totalEBITDA = (Decimal)ag.get('ebitdatot');
                }
                acc.Global_Revenue_Total__c = totalRevenue;
                acc.Global_EBITDA_B4_Total__c = totalEBITDA;
                updateAcc.add(acc);
            }
          }
          update updateAcc;
        
    }
    
}
Test Class
---------------
@isTest
public class TestFinancialsCountTrigger {
    
    public static testMethod void financialCount(){
        
        Account acc = new Account(Name='3M Corporation',Industry='Conglomerate');
        insert acc;
        
        Account acc2 = new Account(Name='3M Corporation2',Industry='Conglomerate');
        insert acc2;
        
        Financials__c fin = new Financials__c(AccountName__c=acc.id,Name='Test',Year__c='FY-2017',Global_Revenue_Total__c=100,Global_EBITDA_Total__c=200);
        insert fin;
        
        Financials__c fin2 = new Financials__c(AccountName__c=acc2.id,Name='Test2',Year__c='FY-2017',Global_Revenue_Total__c=500,Global_EBITDA_Total__c=400);
        insert fin2;
        
        fin.Global_Revenue_Total__c = 500;
        update fin;
        delete fin;
                
        Test.startTest();
        
        List<Financials__c> finlist = [select id from Financials__c where AccountName__c = :acc.Id and Year__c='FY-2017'];
        delete finlist;
        
        Test.stopTest();
        
        
    }

}


my code covered only 88% code coverage, How to cover 100% code coverage.


Regards
Lakshmi
Roopa S 1Roopa S 1
@Amit how to write test class for this
write a batch class for to delete all contacts of accounts where account field: active__c="yes"
Prakhyat sapraPrakhyat sapra
Can anybody help me in this qyestion...

write a batch class in which the contacts of all the accounts should be only 2.if more than 2 then it bocomes 2, or if less then it becomes2???????