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
Roopa S 1Roopa S 1 

hii need to write batch class

need to write a batch class which will delete all contacts whose related accounts are inactive. First create a checkbox on account as 'Active'.
Best Answer chosen by Roopa S 1
CharuDuttCharuDutt
Hii Roopa
try Below Batch Class
public class deleteinActiveAccountContact  implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT id FROM Account where IsActive__c = false');
    }
    public void execute(Database.BatchableContext bc, List<Account> records){
        Set<String> AccIds = new set<String>();
        for(Account Acc : records){
            if(Acc.IsActive__c == false){
                AccIds.add(Acc.id);
            }
        }
       list<Contact> lstCon = [Select id,AccountId From Contact where AccountId In :AccIds ];
        If(lstCon.size()>0){
            delete lstCon;
        }
    }
    public void finish(Database.BatchableContext bc){
        Id job = bc.getJobId();
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Roopa
try Below Batch Class
public class deleteinActiveAccountContact  implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT id FROM Account where IsActive__c = false');
    }
    public void execute(Database.BatchableContext bc, List<Account> records){
        Set<String> AccIds = new set<String>();
        for(Account Acc : records){
            if(Acc.IsActive__c == false){
                AccIds.add(Acc.id);
            }
        }
       list<Contact> lstCon = [Select id,AccountId From Contact where AccountId In :AccIds ];
        If(lstCon.size()>0){
            delete lstCon;
        }
    }
    public void finish(Database.BatchableContext bc){
        Id job = bc.getJobId();
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47
Hi Roopa,
please try below batch class code : 

public class inactiveAccountContactDelete implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT id FROM Account WHERE IsActive__c = false');
    }
    public void execute(Database.BatchableContext bc, List<Account> accountList){
        Set<String> accountIds = new set<String>();
        for(Account eachAccount : accountList){
            if(eachAccount.IsActive__c == false){
                accountIds.add(eachAccount.id);
            }
        }
       List<Contact> contactList = new List<Contact>();
       contactList = [SELECT id, name, AccountId FROM Contact WHERE AccountId In : accountIds];
        If(contactList.size()>0){
            delete contactList;
        }
    }
    public void finish(Database.BatchableContext bc){
        Id job = bc.getJobId();
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Suraj
Roopa S 1Roopa S 1
Thanks a lot @CharuDutt @Suraj Tripathi 47
CharuDuttCharuDutt
Hii Roopa
Please Close Your Query By Marking It As Best Answer If It Helps.So It Also Helps Others In Future 
Thank You!