You need to sign in to do that
Don't have an account?
shwetha prabhakar
Test class for quote pdf
Can anyone help me to write the test class for following apex class
public class QuoteApexClass {
public Id testId {get;set;}
private Quotes__c quoteObj;
public String renderAs { get; set; }
public QuoteApexClass(ApexPages.standardController con){
if(con.getId()!=NULL){
testId = con.getId();
quoteObj = [select id,Name,Email__c,ContactId__c from Quotes__c where id =:testId];
}
}
public PageReference saveQuoteAsPDFandEmail()
{
PageReference pr = Page.QuoteTemplate;
Attachment attachmnt = new Attachment();
Blob pdfBlob;
try {
pdfBlob = pr.getContentAsPDF();
}
catch (VisualforceException e) {
pdfBlob = Blob.valueOf('Some Text');
}
attachmnt.parentId = testId;
attachmnt.Name = 'V - '+ system.Now() + '.pdf';
attachmnt.body = pdfBlob;
insert attachmnt;
Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setFileName(attachmnt.Name);
attach.setBody(attachmnt.body);
attach.setContentType('application/pdf');
semail.setSubject('Quote template '+quoteObj.Name);
List <String> listEmailMembers = new List<String>();
listEmailMembers.add('chandra.s@proseraa.com');
listEmailMembers.add(UserInfo.getUserEmail());
semail.setToAddresses(listEmailMembers);
semail.saveAsActivity = true;
semail.setPlainTextBody('As Discussed over the phone.Please find the quotation attached.Also sign the document and send us back.');
semail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });
Messaging.SendEmailResult [] r1 = Messaging.sendEmail(new messaging.SingleEmailMessage[] {semail});
if (r1[0].success)
{
System.debug('The email was sent successfully.');
} else
{
System.debug('The email failed to send: ' + r1[0].errors[0].message);
}
Task tk = new Task();
tk.ActivityDate = system.today();
tk.Subject = 'Email has been sent with subject name as '+'Quote template '+quoteObj.Name;
tk.Description ='Email has been sent with subject name as '+'Quote template" '+quoteObj.Name + ' " along with an attachment "'+attachmnt.Name+ ' " at '+system.now();
tk.Status = 'completed';
tk.WhatId = testId;
insert tk;
attachment att = new attachment();
att.Name = 'V - '+ system.Now() + '.pdf';
att.body = pdfBlob;
att.ParentId = tk.Id;
insert att;
PageReference pageRef = new PageReference('https://proseraa.lightning.force.com/lightning/r/Quotes__c/'+testId+'/view');
pageRef.setRedirect(true);
return pageRef;
}
public void attachPDF() {
PageReference pdfPage = Page.QuoteTemplate;
Attachment attach = new Attachment();
Blob pdfBlob;
try {
pdfBlob = pdfPage.getContent();
}
catch (VisualforceException e) {
pdfBlob = Blob.valueOf('Some Text');
}
attach.parentId = ApexPages.currentPage().getParameters().get('id');
attach.Name = 'Letter - '+ system.Now() + ' .pdf';
attach.body = pdfBlob;
insert attach;
}
public Boolean getShowPrintLink() {
return ( (renderAs == null) || ( ! renderAs.startsWith('PDF')) );
}
public PageReference print() {
renderAs = 'PDF';
PageReference pdfPage = Page.QuoteTemplate;
Attachment attach = new Attachment();
Blob pdfBlob;
try {
pdfBlob = pdfPage.getContentAsPDF();
}
catch (VisualforceException e) {
pdfBlob = Blob.valueOf('Some Text');
}
attach.parentId = testId;
attach.Name = 'V - '+ system.Now() + '.pdf';
attach.body = pdfBlob;
insert attach;
PageReference pageRef = new PageReference('https://proseraa.lightning.force.com/lightning/r/Quotes__c/'+testId+'/view');
pageRef.setRedirect(true);
return pageRef;
}
}
public class QuoteApexClass {
public Id testId {get;set;}
private Quotes__c quoteObj;
public String renderAs { get; set; }
public QuoteApexClass(ApexPages.standardController con){
if(con.getId()!=NULL){
testId = con.getId();
quoteObj = [select id,Name,Email__c,ContactId__c from Quotes__c where id =:testId];
}
}
public PageReference saveQuoteAsPDFandEmail()
{
PageReference pr = Page.QuoteTemplate;
Attachment attachmnt = new Attachment();
Blob pdfBlob;
try {
pdfBlob = pr.getContentAsPDF();
}
catch (VisualforceException e) {
pdfBlob = Blob.valueOf('Some Text');
}
attachmnt.parentId = testId;
attachmnt.Name = 'V - '+ system.Now() + '.pdf';
attachmnt.body = pdfBlob;
insert attachmnt;
Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setFileName(attachmnt.Name);
attach.setBody(attachmnt.body);
attach.setContentType('application/pdf');
semail.setSubject('Quote template '+quoteObj.Name);
List <String> listEmailMembers = new List<String>();
listEmailMembers.add('chandra.s@proseraa.com');
listEmailMembers.add(UserInfo.getUserEmail());
semail.setToAddresses(listEmailMembers);
semail.saveAsActivity = true;
semail.setPlainTextBody('As Discussed over the phone.Please find the quotation attached.Also sign the document and send us back.');
semail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });
Messaging.SendEmailResult [] r1 = Messaging.sendEmail(new messaging.SingleEmailMessage[] {semail});
if (r1[0].success)
{
System.debug('The email was sent successfully.');
} else
{
System.debug('The email failed to send: ' + r1[0].errors[0].message);
}
Task tk = new Task();
tk.ActivityDate = system.today();
tk.Subject = 'Email has been sent with subject name as '+'Quote template '+quoteObj.Name;
tk.Description ='Email has been sent with subject name as '+'Quote template" '+quoteObj.Name + ' " along with an attachment "'+attachmnt.Name+ ' " at '+system.now();
tk.Status = 'completed';
tk.WhatId = testId;
insert tk;
attachment att = new attachment();
att.Name = 'V - '+ system.Now() + '.pdf';
att.body = pdfBlob;
att.ParentId = tk.Id;
insert att;
PageReference pageRef = new PageReference('https://proseraa.lightning.force.com/lightning/r/Quotes__c/'+testId+'/view');
pageRef.setRedirect(true);
return pageRef;
}
public void attachPDF() {
PageReference pdfPage = Page.QuoteTemplate;
Attachment attach = new Attachment();
Blob pdfBlob;
try {
pdfBlob = pdfPage.getContent();
}
catch (VisualforceException e) {
pdfBlob = Blob.valueOf('Some Text');
}
attach.parentId = ApexPages.currentPage().getParameters().get('id');
attach.Name = 'Letter - '+ system.Now() + ' .pdf';
attach.body = pdfBlob;
insert attach;
}
public Boolean getShowPrintLink() {
return ( (renderAs == null) || ( ! renderAs.startsWith('PDF')) );
}
public PageReference print() {
renderAs = 'PDF';
PageReference pdfPage = Page.QuoteTemplate;
Attachment attach = new Attachment();
Blob pdfBlob;
try {
pdfBlob = pdfPage.getContentAsPDF();
}
catch (VisualforceException e) {
pdfBlob = Blob.valueOf('Some Text');
}
attach.parentId = testId;
attach.Name = 'V - '+ system.Now() + '.pdf';
attach.body = pdfBlob;
insert attach;
PageReference pageRef = new PageReference('https://proseraa.lightning.force.com/lightning/r/Quotes__c/'+testId+'/view');
pageRef.setRedirect(true);
return pageRef;
}
}
Try this,
I hope this helps you.