+ Start a Discussion
MaggieSumitMaggieSumit 

getting error in test class for Attachment Email Apex class

  1. @isTest
  2. private class OpportunityMatchingHelperTest {
  3.     
  4.     private static testMethod void testCreateRecord() {
  5.         
  6.     Id conRecID = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Student').getRecordTypeId(); 
  7.     Id oppRecID = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Opportunity').getRecordTypeId();  
  8.     
  9.         Test.startTest();
  10.         
  11.         Account acc = new Account(Name = 'test');
  12.         Insert acc;
  13.         
  14.         Contact con = new Contact(Lastname = 'test con',Email = 'test@gmail.com',AccountId = acc.Id,Academic_Standing__c = 'Good Standing',recordTypeId = conRecID);
  15.         Insert con;
  16.         
  17.         Opportunity opp = new Opportunity(AccountId = acc.Id,recordTypeId = oppRecID,Name = 'Demo',StageName = 'Demo',CloseDate = System.Today(),Key_Contact__c = con.Id);
  18.         Insert opp;
  19.         
  20.         alu_Opportunity_Matching__c aluOpp = new alu_Opportunity_Matching__c(Opportunity__c = opp.Id,Applicant_Student_Record__c = con.Id, Application_Status__c = 'Potential Candidate');
  21.         Insert aluOpp;
  22.         
  23.         Blob b = Blob.valueOf('Test Data'); 
  24.         
  25.         Attachment attachment = new Attachment(ParentId = opp.id,Name = 'Test Attachment for Parent',Body = b);  
  26.         Insert attachment;
  27.         
  28.         aluOpp.Application_Status__c = 'Pitched';
  29.         Update aluOpp;
  30.  
  31.         Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
  32.         efa.setFileName( attachment.Name);
  33.         efa.setBody(b); 
  34.         
  35.          List<Attachment> attList = new List<Attachment>();
  36.             for(Integer i=0;i<6;i++){
  37.              Attachment Attac =new Attachment();
  38.              Attac.Name='Unit Test Attachment';
  39.              Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
  40.              Attac.body=bodyBlob;
  41.              Attac.parentId=opp.id;
  42.              attList.add(Attac);
  43.             }
  44.          insert attList;             
  45.     }        
  46. }
User-added image