You need to sign in to do that
Don't have an account?
Hanna Bergsma
Test Class to cover Messaging.Emailfileattachment
I am short coverage when I deploy to production by 5%. Im at 88% total for this trigger, please, can anyone help?
Here is my trigger
This is what isnt covered currently
Here is my test class with 88% Coverage
Here is my trigger
trigger filingSuccessfulEmailWithDocs on Case (after update) { //still need text in body of email, template, criteria for trigger OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress where Displayname='Name' limit 1]; String subject = 'Subject'; String plainTextBody ='Email text here'; Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); for(Case myCase : trigger.new){ Case oldCase = Trigger.oldMap.get(myCase.Id); if(myCase.RecordTypeName__c=='Order' && myCase.Type == 'Filings' && myCase.Email_Client_Attached_Docs__c==True && myCase.Email_Client_Attached_Docs__c != oldCase.Email_Client_Attached_Docs__c){ String requirements = myCase.Report_Requirements__c; String plainTextBody =''; plainTextBody += // Get ContentVersion List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>(); for (ContentDocumentLink docLinks : [Select Id, ContentDocumentId from ContentDocumentLink where LinkedEntityId = :myCase.id ]) { for (ContentDocument docs : [Select Id, FileType, Title, FileExtension from ContentDocument where Id= :docLinks.ContentDocumentId]) { for (ContentVersion docVersion : [Select Id, VersionData from ContentVersion where ContentDocumentId =:docLinks.ContentDocumentId ]) { Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment(); string fileName = docs.Title.Trim() + '.' + docs.FileExtension.Trim(); efa.setFileName(fileName); //Title of the PDF efa.setBody(docVersion.VersionData); //Body of the PDF,need to do transfer into blob fileAttachments.add(efa); } email.setFileAttachments(fileAttachments); } } //add address's that you are sending the email to List<String> sendTo = new List<String>(); String owneremail = [select Email from User where Id = :myCase.OwnerId].Email; sendTo.add(myCase.ContactEmail); //sendTo.add(owneremail); email.setSubject(subject); String[] toAddresses = (sendto); email.setPlainTextBody(plainTextBody); email.setToAddresses( toAddresses ); email.setReplyTo('DoNotReply@xxx.com'); email.setOrgWideEmailAddressId(owa.id); email.setSaveAsActivity(true); // Sends the email Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); EmailMessage em = new EmailMessage(); em.FromAddress = owa.Address; em.FromName = 'DoNotReply'; em.HtmlBody = email.getHtmlBody(); em.Subject = email.Subject; em.Incoming = false; em.Status = '3'; em.ParentId = myCase.id; em.TextBody = email.getPlainTextBody(); em.FromAddress = owa.address; em.ToAddress = myCase.ContactEmail; em.ValidatedFromAddress = owa.Address; insert em; EmailMessageRelation emr = new EmailMessageRelation(); emr.emailMessageId = em.Id; emr.relationId = myCase.ContactId; emr.relationType = 'ToAddress'; // OtherAddress, FromAddress,.... insert emr; } }
This is what isnt covered currently
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>(); for (ContentDocumentLink docLinks : [Select Id, ContentDocumentId from ContentDocumentLink where LinkedEntityId = :myCase.id ]) { for (ContentDocument docs : [Select Id, FileType, Title, FileExtension from ContentDocument where Id= :docLinks.ContentDocumentId]) { for (ContentVersion docVersion : [Select Id, VersionData from ContentVersion where ContentDocumentId =:docLinks.ContentDocumentId ]) { Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment(); string fileName = docs.Title.Trim() + '.' + docs.FileExtension.Trim(); efa.setFileName(fileName); //Title of the PDF efa.setBody(docVersion.VersionData); //Body of the PDF,need to do transfer into blob fileAttachments.add(efa); } email.setFileAttachments(fileAttachments); }
Here is my test class with 88% Coverage
@isTest private class filingEmailWithDocsTest { @isTest static void createTest() { Contact con = New Contact(); con.FirstName = 'Hanna'; con.LastName = 'Banana'; con.Email = 'hbanana@aol.com'; insert con; State_Information__c state = New State_Information__c(); state.state_code__c = 'MI'; state.Name = 'Michigan'; insert state; Case myCase = new Case(); myCase.Subject = 'Hanna'; myCase.ContactId=con.id; myCase.State_Filed_In1__c= state.Id; myCase.Status='Closed - Resolved'; myCase.Type='Filings'; myCase.Email_Client_Attached_Docs__c=False; myCase.RecordTypeId='012F0000000WjvwIAC'; insert myCase; Case caseToUpdate; caseToUpdate=[SELECT id from Case where id=: myCase.Id]; CaseToUpdate.Email_Client_Attached_Docs__c=TRUE; update caseToUpdate; //Create Attachment for testing Attachment Attachment =new Attachment(); Attachment.Name='Unit Test Attachment'; Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); Attachment.body=bodyBlob; Attachment.parentId=myCase.id; insert Attachment ; ContentVersion contentVersion = new ContentVersion( Title = 'Penguins', PathOnClient = 'Penguins.jpg', VersionData = Blob.valueOf('Test Content'), IsMajorVersion = true ); insert contentVersion; List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument]; //create ContentDocumentLink record ContentDocumentLink cdl = New ContentDocumentLink(); cdl.LinkedEntityId = myCase.id; cdl.ContentDocumentId = documents[0].Id; cdl.shareType = 'V'; insert cdl; } }
Sree07
Did you find any solution for this?
Hanna Bergsma
I removed other triggers from my org that were at lower coverage to solve for the total coverage issue