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
Manoj DegaManoj Dega 

Delete Account Related Attachments Using Batch Apex through Custom Settings

Hi All,
I'm Strucking on Here. When I Run my Test Class Its Fail.

Batch Class
global class DeleteAttachmentScheduleClass implements Schedulable{
    public String  accstr1;
    public String  accstr2;
    public list<Account> acclist1 { get; set; }
    public list<Account> acclist2 { get; set; }
    public list<Account> acclistall { get; set; }
    public Set<Id> setAttachmetIds{ get; set; }
    public String  query;    

    global void execute(SchedulableContext sc){   
         acclist1 = new list<Account>();
         acclist2 = new list<Account>();
         acclistall = new list<Account>();
	
	//Custom Settins
        AccountSetting__c ecs =AccountSetting__c.getInstance();
        integer read=integer.valueOf(ecs.Read__c); // Read__c = 10
        integer unread=integer.valueOf(ecs.Unread__c); //Unread__c = 20

        accstr1='select id,name,is_read__c,(select id from Attachments where createddate < last_n_days :'+unread+') from Account where is_read__C = false';
        accstr2='select id,name,is_read__c,(select id from Attachments where createddate < last_n_days :'+read+') from Account where is_read__C = true';
        
        acclist1 = database.query(accstr1);
        acclist2 = database.query(accstr2);
        acclistall.addall(acclist1);
        acclistall.addall(acclist2); 
        for (Account objAccount:acclistall) {
            if (objAccount.attachments != null && objAccount.attachments.size() > 0) {
                for (Attachment objAttachment:objAccount.attachments) {
                    setAttachmetIds.Add(objAttachment.Id);
                    
                }
            }
        }

        query = 'select id from attachment where id in :setAttachmetIds';
              
        list<Attachment> attlist = database.query(query);
        if(attlist != null){
          try{
                DeleteAttachmentsBatchClass delBatch = new DeleteAttachmentsBatchClass(query);
                Id BatchProcessId = Database.ExecuteBatch(delBatch);
          }catch(Exception e){
                
          }
        }    
    }
}

Test Class
@isTest
public class TestDeleteAttachmentSchedule {
    public static String CRON_EXP = '0 0 0 15 3 ? 2022';
    Public static testmethod void test() {
        
        Test.startTest();
        Case c = new Case(); // For Case_Number__c is Required
        insert c;
        AccountSetting__c acs = new AccountSetting__c(Unread__c = 10, Read__c = 20);
        insert acs;
        Account acc = new Account (Case_Number__c = c.id,is_Read__c = TRUE);
        insert acc;
         List<Attachment> attachments=[select id, name from Attachment where parent.id=:ems.id];
        //System.assertEquals(1, attachments.size());
        delete attachments;
        String jobId = System.schedule('ScheduleApexClassTest23',CRON_EXP,new DeleteAttachmentScheduleClass22());
        Test.stopTest();       
    }
   
}

User-added image
sfdc550sfdc550
In what line you are getting an error in class??

You need to perform a Null Pointer check on the line you are getting error
eg: In if condition !=null

I think you can overcome the problem