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
XIOXIO 

Has Attachment Trigger / Test Class Code Coverage HELP!

Hello,

I have the Apex Trigger below the checkmarks the Has_Attachement__c field on the Case record when there in an attachement on the case BUT I'm only getting 50% code coverage with the Test Class below. I need help from the community to assist me on increasing the code coverage so that I can deploy the trigger into production. Any assistance is greatly appreciated, thank you!

Apex Trigger 
trigger CaseHasAttachement on Attachment (after insert) {
            {
                        List<Case> cs = [select id, Has_Attachment__c from Case where id =: Trigger.New[0].ParentId];
                        if(cs.size()>0)
                        {
                                    cs[0].Has_Attachment__c = true;
                                    update cs;
                        }                                                                   
            }
            }

Test Class
@isTest
public class CaseHasAttachement_UT {

    
    static testMethod void myTest() {
    
            Account a=new Account();
        a.Name='test';
        a.Type='Member';
        a.Major_Market__c='Other';
        a.AnnualRevenue=1000;
        insert a;  
        
        Contact c=new Contact();
        c.RecordTypeID='01250000000HdFN';
        c.FirstName='ferasfd';
        c.LastName='testeringglowoski';
        c.phone='123456';
        c.AccountId=a.id;
        c.Level_in_Account__c='VP';
        c.Primary_Job_Function__c='Unknown';
        c.LeadSource='Sales';
        insert c;
        
        Case testCase = TestDataUtils.createCase(false);
        testCase.ContactId = c.id;
        testCase.AccountId = a.id;
        testCase.Web_Primary_Service_Discipline__c = 'Field Services';
        testCase.Type = 'Research Inquiry';
        testCase.Has_Attachment__c = FALSE;
        insert testCase;
    
        Attachment attach=new Attachment(); 
        attach.Name='Unit Test Attachment'; 
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); 
        attach.body=bodyBlob; 
        attach.parentId=a.Id; 
        attach.ContentType = 'application/msword'; 
        attach.IsPrivate = false; 
        attach.Description = 'Test'; 
        insert attach; 
        System.debug('Inserted: '+ attach.Id); 
        
    }
}

 
Best Answer chosen by XIO
XIOXIO
I figured it out. Changed the attach.parentID = a.ID to attach.parentID = testCase.ID
Got a 100% code coverage!