You need to sign in to do that
Don't have an account?
Phuc Nguyen 18
Email Test Class
Will someone help me with writing a test class for my email class?
Thank you,
P
public with sharing class PDF_EmailController { @auraEnabled public static void SendAttachment(String recordId){ for(Purchase_Order_Request__c por : [SELECT Id, Email_Address__c FROM Purchase_Order__c WHERE Id =: recordId]){ Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage(); Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment(); PageReference pref = page.POPDF; pref.getParameters().put('Id',por.Id); pref.setRedirect(true); Blob b = pref.getContent(); attach.setFileName('POR.pdf'); attach.setBody(b); semail.setSubject('Purchase Order Request Details'); semail.setToAddresses(new String[] { por.Email_Address__c }); semail.setPlainTextBody('Please find the attached POR details'); semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach}); Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail}); } for(Purchase_Order_Request__c por : [SELECT Id, Email_Address__c,Version__c FROM Purchase_Order__c WHERE Id =: recordId]){ PageReference PDf = Page.PORequestPDF; PDf.getParameters().put('Id',por.Id); PDf.setRedirect(true); Attachment attach = new Attachment(); Blob b ; b = PDf.getContent(); attach.Body = b; attach.Name = 'Purchase Order Confirmation ' + System.today(); attach.IsPrivate = false; attach.ParentId = por.Id; insert attach; } } } Visualforce Page <apex:page controller="PDF_EmailController" standardStylesheets="false" showHeader="false"> <center> <P style="font-size:22px;"><u>Send Purchase Order Request</u></P> <input type="text" value="" id="txtEmailAddress"/><br/><br/> <input type="button" onclick="SendEmailAttachment();" value="Send"/> </center> <script> function SendEmailAttachment(){ var PORId = '{!$CurrentPage.parameters.Id}'; var emailAddress=document.getElementById('txtEmailAddress').value; if(emailAddress!=null && emailAddress!=''){ TestHandler.SendAttachment(emailAddress,porId,function(result,event){ if(event.status){ if(result=='SUCCESS'){ alert('Email sent successfully.'); } else{ alert(result); } } }) } else{ alert('Please provide email address!'); } } </script> </apex:page>
Thank you,
P
Follow the below changes in Apex Class also because the getContent() pdf method won't support in testclass. So we have to include the Test.IsRunningTest() check in actual class.
Test Class:
Add all the required fields below to create the Purchase_Order_Request__c Record also if any parent records needed insert those also.
Thanks,
Maharajan.C
All Answers
Follow the below changes in Apex Class also because the getContent() pdf method won't support in testclass. So we have to include the Test.IsRunningTest() check in actual class.
Test Class:
Add all the required fields below to create the Purchase_Order_Request__c Record also if any parent records needed insert those also.
Thanks,
Maharajan.C
Cheers,
P