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
Oksana DovbakOksana Dovbak 

Apex Class Test for Attachment Trigger

Hello,

I'm trying to create an apex class to use in testing an attachment trigger I created below - 

 
//Trigger to update the Send_Attachment_Email__c custom checkbox field on Opportunity if it has an attachment.

trigger attachmentTrigger on Attachment (after insert) {

list<Opportunity> o = [select id,   Send_Attachment_Email__c, StageName from Opportunity where id =: Trigger.New[0].ParentId] ;
    if(o.size()>0 ) {
        o[0].Send_Attachment_Email__c = true;
        update o;
    }
    }



Could you please advise me on the apex class needed to make sure this satisfies the code coverage?
Best Answer chosen by Oksana Dovbak
sfdcMonkey.comsfdcMonkey.com
Hi Oksana, use below test class for 100% code coverge
@isTest 
public class AttachTriggerTest {
    static testMethod void testMethod1(){
        Opportunity opp = new Opportunity();
          
            opp.Name = 'Test Opportunity';
            opp.CloseDate = system.today();
            opp.StageName = 'Closed-Won';
            opp.Type = 'New Customer';
            opp.Send_Attachment_Email__c = false ;
          // add all requred fields here for opportunity
        insert opp;
        
        Attachment attach=new Attachment();       
         attach.Name='Unit Test Attachment';
         Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
          attach.body = bodyBlob;
          attach.parentId = testOpportunity.id;
        insert attach;
        
        }
}

Let me inform if it helps you and kindly mark it best answer if it helps you
thanks

All Answers

Karthik KKarthik K
Hi Oksana Dovbak,

Here is the sample Code. Please try it out and let me know.
 
@isTest
public class AttachmentTest{
 
private static testmethod testAttachment(){

Opportunity testOpportunity = new Opportunity(
RecordTypeID = rt.Id,
Name = 'Test Opportunity',
CloseDate = '2018-01-01',
StageName = 'Closed-Won',
TimeZoneSidKey = 'America/Chicago',
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US' );
insert testOpportunity;

Attachment att = new Attachment(Name='test',
ParentId=testOpportunity.id);
insert att;
}
}

 
sfdcMonkey.comsfdcMonkey.com
Hi Oksana, use below test class for 100% code coverge
@isTest 
public class AttachTriggerTest {
    static testMethod void testMethod1(){
        Opportunity opp = new Opportunity();
          
            opp.Name = 'Test Opportunity';
            opp.CloseDate = system.today();
            opp.StageName = 'Closed-Won';
            opp.Type = 'New Customer';
            opp.Send_Attachment_Email__c = false ;
          // add all requred fields here for opportunity
        insert opp;
        
        Attachment attach=new Attachment();       
         attach.Name='Unit Test Attachment';
         Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
          attach.body = bodyBlob;
          attach.parentId = testOpportunity.id;
        insert attach;
        
        }
}

Let me inform if it helps you and kindly mark it best answer if it helps you
thanks
This was selected as the best answer
Oksana DovbakOksana Dovbak
Thank you Piyush - I just had to change line 18 to "attach.parentId = opp.id;"