+ Start a Discussion
shweta raghav 13shweta raghav 13 

how to write test class for the below code of email and attachment

if(oppList.size()>0){
               for(Opportunity opp : oppList){
                   if(opp.StageName== '100% - Closed Won'){                    
                       Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                       message.toAddresses = new String[] {'sh@gmail.com'};
                       message.optOutPolicy = 'FILTER';
                       message.setSubject(' New attachment on Opportunity : ' + '  ' + opp .name+'    ' +'with probability 100% for Account'+'  ' + opp.Account.Name);
                       message.setHtmlBody ('Hi Team, <br/><br/> There is a new attachment for opportunity '+opp .name+'  Please review the attached document<br/><br/><a href=&#34;https://c3.salesforce.com/'+opp.id+'&#34;>https://c3.salesforce.com/'+opp.id+'</a><br/><br/>Regards<br/><br/>Salesforce Admin');
              
                      for(Attachment a : trigger.new){
                           if(a.parentId == opp.id){
                                Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                                efa.setFileName(a.Name);
                                efa.setBody(a.Body);
                                efa.setContentType(a.ContentType);
                                fileAttachments.add(efa);
                          }
                      }
                          if(!fileAttachments.isEmpty()){
                                Message.setFileAttachments(fileAttachments);
                                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {message });
                          }
Waqar Hussain SFWaqar Hussain SF
Hi,
Try below code snippet
 
@isTest
public class MyTestClass1{		  
	
	public static testmethod void MyUnitTest1(){
		Account acc = new Account();
		acc.Name = 'Test';
		insert acc;
		
		Opportunity opp = new Opportunity();
		opp.Name = 'Test opp';
		opp.ClosedDate = date.today.addDays(30);
		opp.StageName = '100% - Closed Won';
		insert opp;
		
		attachment att = new attachment();
		att.Name = 'test att';
		att.ParentId = opp.Id;
		att.Body = blob.valueOf('test');
		insert att;
	}
}

 
shweta raghav 13shweta raghav 13
i have already create data for aaccount ,contact,opportunity and attachment but not able to cover above code 
Waqar Hussain SFWaqar Hussain SF
How are you querying oppList in your trigger.
Can you share the whole code.
shweta raghav 13shweta raghav 13
trigger SendAttachment on Attachment (before insert, After Insert) {
if(trigger.IsInsert)    {
       if(trigger.IsBefore){
           set<id> parentIds = New set<id>();
           List <String> attachName = new List<string>();
           for(attachment att : trigger.new){
               if (att.parentId !=null){
                   system.debug('att.parentId==='+att.parentId);
                   parentIds.add(att.parentId);
                   system.debug('att.attachName==='+att.Name);
                   attachName.add(att.name);
               }    
           }
           if(parentIds!=null){
               system.debug('parentIds---'+attachName+'parentIds --- sec----'+parentIds);
               List<attachment> attachlist = new List<attachment>();
               attachlist=[SELECT Id, name FROM  Attachment WHERE ParentId In : parentIds AND Name =: attachName];
               system.debug('attachlist()--'+attachlist);
               if(!attachlist.isEmpty()){
                   delete attachlist;
               }
           }
       }if(trigger.IsAfter){
           set<id> parentIds = New set<id>();
           for(attachment att : trigger.new){
           String parentIdString = String.valueof(att.parentId);
               if (att.parentId !=null && parentIdString.substring(0,3) == '006'){
                   parentIds.add(att.parentId); 
               }
           }
           List<Opportunity> oppList = New list <Opportunity>([SELECT Id,StageName,Name,Account.Name FROM Opportunity WHERE Id IN : parentIds]);
           List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();

           if(oppList.size()>0){
               for(Opportunity opp : oppList){
                   if(opp.StageName== '100% - Closed Won'){                    
                       Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                       message.toAddresses = new String[] {'s@gmail.com'};
                       message.optOutPolicy = 'FILTER';
                       message.setSubject(' New attachment on Opportunity : ' + '  ' + opp .name+'   ' +'with probability 100% for Account'+'  ' + opp.Account.Name);
                       message.setHtmlBody ('Hi Team, <br/><br/> There is a new attachment for opportunity '+opp .name+'  .Please review the attached document<br/><br/><a href=&#34;https://c3.salesforce.com/'+opp.id+'&#34;>https://c3.salesforce.com/'+opp.id+'</a><br/><br/>Regards<br/>Salesforce Admin');
              
                      for(Attachment a : trigger.new){
                           if(a.parentId == opp.id){
                                Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                                efa.setFileName(a.Name);
                                efa.setBody(a.Body);
                                efa.setContentType(a.ContentType);
                                fileAttachments.add(efa);
                          }
                      }
                          if(!fileAttachments.isEmpty()){
                                Message.setFileAttachments(fileAttachments);
                                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {message });
                          }
                    }         
               }            
           }
       }
   }
}
Waqar Hussain SFWaqar Hussain SF
Is your trigger working as expected, I mean is your trigger is sending email?
You are first deleting attachments and then getting opportunities using attachment parentId.

By the way, try the below code. Test class seems fine, the issue can be with your trigger logic. 
 
@isTest
public class MyTestClass1{          
    
    public static testmethod void MyUnitTest1(){
        Account acc = new Account();
        acc.Name = 'Test';
        insert acc;
        
        Opportunity opp = new Opportunity();
        opp.Name = 'Test opp';
        opp.ClosedDate = date.today.addDays(30);
        opp.StageName = '100% - Closed Won';
        insert opp;
        
        attachment att = new attachment();
        att.Name = 'test att';
        att.ParentId = opp.Id;
        att.Body = blob.valueOf('test');
        
        test.startTest();
        insert att;
        test.stopTest();
    }
}

 
shweta raghav 13shweta raghav 13
in trigger ,there is 2 functionality.1 sending mail when stage 100.and 2, delete the existing attachment with new one if attachment have same name.
Waqar Hussain SFWaqar Hussain SF
Yeah, but according to Salesforce trigger order of execution, Salesforce first executes all before triggers and then executes all after triggers.

So first trigger deletes all the attachments and then send emails.