You need to sign in to do that
Don't have an account?
how to write test Method for Email service
can anybody help me to write Test Method for the given code below :
public PageReference sendEmail()
{
ApexPages.Message sendMessage= null;ApexPages.Message sendErrorMessage= null;Messaging.SendEmailResult[] mailResult=
null;PageReference pages=null;
try
{
this.customerId = ApexPages.currentPage().getParameters().get('customerIdForLookup');PageReference ForecastPdfPage=null;String thisFNAReport = ApexPages.currentPage().getParameters().get(
'Id');customerContact = [select id,Email__c,First_Name__c,Last_Name__c from
CustomerContactDetail__c where id = :customerId];Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage ();
if(fnaReport.Report_Type__c =='FNA Report'){
ForecastPdfPage = Page.FNAReportAsPdf;
ForecastPdfPage.getParameters().put('Id',customerContact.id);Blob ForecastPdfBlob = ForecastPdfPage.getContent();
Attachment a = new Attachment(parentId=customerContact.id,name=customerContact.First_Name__c + '-'+ customerContact.Last_Name__c+'-'+'FNAReport.pdf', body = ForecastPdfBlob); insert a;email.setSaveAsActivity(true);
email.setToAddresses(new String[]{customerContact.email__c});email.setSenderDisplayName(
'FNA Report');email.setSubject('FNA Report For ' + customerContact.First_Name__c + ' '+customerContact.Last_Name__c);
email.setHtmlBody('Hi,' + ' '+'<br>'+'<b>'+' '+customerContact.First_Name__c +' '+'<br><br>'+'</b>'+'Your FNA Report is attached with this mail'+'<br>'+'<br><br>'+'Thanks'); // Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); efa.setFileName('FNAReport.pdf');efa.setBody(ForecastPdfBlob);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); mailResult = Messaging.sendEmail(
new Messaging.SingleEmailMessage[] {email});for(Messaging.SendEmailResult er : mailResult)
if(er.isSuccess()){
sendMessage=new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Mail Sucessfully Sent to ' + customerContact.email__c);ApexPages.addMessage(sendMessage);
}
}
else if(fnaReport.Report_Type__c=='Retirement Report')
{
ForecastPdfPage = Page.RetirementReportAsPdf;
ForecastPdfPage.getParameters().put('Id',customerContact.id);Blob ForecastPdfBlob = ForecastPdfPage.getContent();
Attachment a = new Attachment(parentId=customerContact.id,name=customerContact.First_Name__c + '-'+ customerContact.Last_Name__c+'-'+'RetirementReport.pdf', body = ForecastPdfBlob); insert a;email.setSaveAsActivity(true);
email.setToAddresses(new String[]{customerContact.email__c});email.setSenderDisplayName(
'Retirement Report');system.debug('***** Customer Name *****'+customerContact.First_Name__c); email.setSubject(
'Retirement Report For ' + customerContact.First_Name__c + ' '+customerContact.Last_Name__c);email.setHtmlBody('Hi,' + ' '+'<br>'+'<b>'+' '+customerContact.First_Name__c +' '+'<br><br>'+'</b>'+'Your Retirement Report is attached with this mail'+'<br>'+'<br><br>'+'Thanks'); // Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); efa.setFileName('RetirementReport.pdf');efa.setBody(ForecastPdfBlob);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); mailResult = Messaging.sendEmail(
new Messaging.SingleEmailMessage[] {email});for(Messaging.SendEmailResult er : mailResult)
if(er.isSuccess()){
sendMessage=new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Mail Sucessfully Sent to ' + customerContact.email__c);ApexPages.addMessage(sendMessage);
}
}
}
catch(Exception e){
sendErrorMessage=new ApexPages.Message(ApexPages.Severity.ERROR, 'Unable to send mail to' + customerContact.email__c);ApexPages.addMessage(sendMessage);
}
//return controller.view();
return pages;
}
Thnaks in advance
Madhulendra