You need to sign in to do that
Don't have an account?
Rahul
can someone help me with the test class of this apex Email code?
public class sendofferlettercontroller {
public sendofferlettercontroller() { }
public String toEmail {get;set;}
public String ccEmail {get;set;}
Public ID ParentID{get;set;}
public String subject {get;set;}
public String body {get;set;}
private final contact MyLead;
public static contact co1{get;set;}
public sendofferlettercontroller(ApexPages.StandardController controller)
{
this.MyLead=(Contact)controller.getRecord();
ParentID = apexpages.currentpage().getparameters().get('id');
}
public PageReference sendEmail() {
co1 = [select id ,name, Email from contact where id =: ApexPages.currentPage().getParameters().get('id')];
//create a mail object to send email
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toaddress = toEmail.Split(','); //using comma separator between emails
//String[] toaddress = toEmail.Split('[,;]'); //use can use Regex to mix with comma & semi colon separator
//email properties
String[] ccaddress = ccEmail.Split(',');
mail.setToAddresses(toaddress);
mail.setSubject(subject);
mail.setccAddresses(ccaddress);
mail.setUseSignature(true);
mail.setPlainTextBody(body);
// send the email'
PageReference pdf = Page.offerletterpage;
pdf.getParameters().put('id',(String)co1.id);
pdf.setRedirect(true);
blob b;
try{
b = pdf.getContent();
}catch (VisualforceException e){
b = Blob.valueOf('Some Text');
}
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('ImpactGuru Offer Letter PDf'+'.pdf');
efa.setBody(b);
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
try
{
// Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
catch(System.EmailException ex)
{
}
Datetime myDatetime = Datetime.now();
String myDatetimeStr;
Attachment a = new Attachment();
a.parentid = co1.id;
a.body = b;
a.name = (co1.Name+' - '+myDatetime.format('d - MMMM, yyyy'));
insert a;
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
Return Page.sendofferletter;
}
}
Thanks in advance
public sendofferlettercontroller() { }
public String toEmail {get;set;}
public String ccEmail {get;set;}
Public ID ParentID{get;set;}
public String subject {get;set;}
public String body {get;set;}
private final contact MyLead;
public static contact co1{get;set;}
public sendofferlettercontroller(ApexPages.StandardController controller)
{
this.MyLead=(Contact)controller.getRecord();
ParentID = apexpages.currentpage().getparameters().get('id');
}
public PageReference sendEmail() {
co1 = [select id ,name, Email from contact where id =: ApexPages.currentPage().getParameters().get('id')];
//create a mail object to send email
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toaddress = toEmail.Split(','); //using comma separator between emails
//String[] toaddress = toEmail.Split('[,;]'); //use can use Regex to mix with comma & semi colon separator
//email properties
String[] ccaddress = ccEmail.Split(',');
mail.setToAddresses(toaddress);
mail.setSubject(subject);
mail.setccAddresses(ccaddress);
mail.setUseSignature(true);
mail.setPlainTextBody(body);
// send the email'
PageReference pdf = Page.offerletterpage;
pdf.getParameters().put('id',(String)co1.id);
pdf.setRedirect(true);
blob b;
try{
b = pdf.getContent();
}catch (VisualforceException e){
b = Blob.valueOf('Some Text');
}
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('ImpactGuru Offer Letter PDf'+'.pdf');
efa.setBody(b);
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
try
{
// Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
catch(System.EmailException ex)
{
}
Datetime myDatetime = Datetime.now();
String myDatetimeStr;
Attachment a = new Attachment();
a.parentid = co1.id;
a.body = b;
a.name = (co1.Name+' - '+myDatetime.format('d - MMMM, yyyy'));
insert a;
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
Return Page.sendofferletter;
}
}
Thanks in advance
private class sendOfferLetterControllerTest {
static testMethod void sendOfferLetterControllerTest() {
//insert Account
Account objAccount = new Account();
objAccount.name = 'test Account';
insert objAccount;
Contact objContact = new Contact();
objContact.accountId = objAccount.id;
objContact.lastName = 'lastName';
objContact.firstName = 'firstName';
insert objContact;
ApexPages.StandardController objStdController = new ApexPages.StandardController(objContact);
//pass the parameter in the url
ApexPages.currentPage().getParameters().put('id', objContact.id);
//Initialize the controller
SendOfferLetterController clsSendOfferLtrCntrl = new SendOfferLetterController(objStdController);
// set the parameters - toEmail, ccEmail and body.
clsSendOfferLtrCntrl.toEmail = 'testemail@test.com';
clsSendOfferLtrCntrl.ccEmail = 'testemail2@test.com';
clsSendOfferLtrCntrl.body = 'This is test body';
//invoke sendEmail method.
clsSendOfferLtrCntrl.sendEmail();
}
}
All Answers
private class sendOfferLetterControllerTest {
static testMethod void sendOfferLetterControllerTest() {
//insert Account
Account objAccount = new Account();
objAccount.name = 'test Account';
insert objAccount;
Contact objContact = new Contact();
objContact.accountId = objAccount.id;
objContact.lastName = 'lastName';
objContact.firstName = 'firstName';
insert objContact;
ApexPages.StandardController objStdController = new ApexPages.StandardController(objContact);
//pass the parameter in the url
ApexPages.currentPage().getParameters().put('id', objContact.id);
//Initialize the controller
SendOfferLetterController clsSendOfferLtrCntrl = new SendOfferLetterController(objStdController);
// set the parameters - toEmail, ccEmail and body.
clsSendOfferLtrCntrl.toEmail = 'testemail@test.com';
clsSendOfferLtrCntrl.ccEmail = 'testemail2@test.com';
clsSendOfferLtrCntrl.body = 'This is test body';
//invoke sendEmail method.
clsSendOfferLtrCntrl.sendEmail();
}
}