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
Porty 23110Porty 23110 

Test Class coverage on Case attachment

I have a trigger that prompts the user to include an attachment before saving a case record. Can someone please help me with the test class that provides a 100% code coverage. 
trigger CaseAttachment on Case (before update) {
    
    for (Case myCase : Trigger.New) {
        if (myCase.Status == 'Repair Confirmed') {
            
            Attachment myAttachment = new Attachment();
            
            try {
                myAttachment = [Select Id, Name from Attachment where ParentId = :myCase.Id];
            }
         
            catch (Exception e)  {
                myAttachment = null;
            } 
                
                
                if (myAttachment ==null)
                    myCase.addError ('Add picture to the record');
                
                
            }
            
            
                
        }
        
        
    }
Best Answer chosen by Porty 23110
Ajay K DubediAjay K Dubedi
Hi Porty,

Test Class coverage on Case attachment. This test class is 100% code coverage.
 
@isTest
public class CaseAttachmentTest {
    @isTest public static void CaseAttachmentMethod()
    {
        Case caseObj = new Case();
        caseObj.Status = 'Closed';
        caseObj.Origin = 'Phone';
        insert caseObj;
        
        Attachment attachmentObj = new Attachment();
        attachmentObj.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attachmentObj.body=bodyBlob;
        attachmentObj.parentId=caseObj.id;
        insert attachmentObj;
        
        Case caseUpdateOject = new Case();
        caseUpdateOject = [Select Status,Origin from Case where Id = :caseObj.Id];
        caseUpdateOject.Status= 'Repair Confirmed';
        update caseUpdateOject;
    }
    @isTest public static void CaseAttachmentMethodException()
    {
        Case caseObj = new Case();
        caseObj.Status = 'Closed';
        caseObj.Origin = 'Phone';
        insert caseObj;
        
        Case caseUpdateOject = new Case();
        caseUpdateOject = [Select Status,Origin from Case where Id = :caseObj.Id];
        caseUpdateOject.Status= 'Repair Confirmed';
        update caseUpdateOject;
    }

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com 

All Answers

Ajay K DubediAjay K Dubedi
Hi Porty,

Test Class coverage on Case attachment. This test class is 100% code coverage.
 
@isTest
public class CaseAttachmentTest {
    @isTest public static void CaseAttachmentMethod()
    {
        Case caseObj = new Case();
        caseObj.Status = 'Closed';
        caseObj.Origin = 'Phone';
        insert caseObj;
        
        Attachment attachmentObj = new Attachment();
        attachmentObj.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attachmentObj.body=bodyBlob;
        attachmentObj.parentId=caseObj.id;
        insert attachmentObj;
        
        Case caseUpdateOject = new Case();
        caseUpdateOject = [Select Status,Origin from Case where Id = :caseObj.Id];
        caseUpdateOject.Status= 'Repair Confirmed';
        update caseUpdateOject;
    }
    @isTest public static void CaseAttachmentMethodException()
    {
        Case caseObj = new Case();
        caseObj.Status = 'Closed';
        caseObj.Origin = 'Phone';
        insert caseObj;
        
        Case caseUpdateOject = new Case();
        caseUpdateOject = [Select Status,Origin from Case where Id = :caseObj.Id];
        caseUpdateOject.Status= 'Repair Confirmed';
        update caseUpdateOject;
    }

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com 
This was selected as the best answer
Porty 23110Porty 23110
Hi Ajay, thank you so much. Your solution worked
Porty 23110Porty 23110
Hi Ajay, i was hoping you could help me with the trigger. Everything works fine in checking for an attachment. Problem is, after inserting the attachment and wanting to save the record, i still get an error asking me to add attachment to the record. Cab you review for me please?