Hey guys, today in this post we are going to learn about How to create Apex Tests Unit for create pdf from visualforce page and save as attachment in Salesforce.
This page is a simple user interface. When youβre generating a PDF file from Apex, all the action is in the Apex code. To know more about Render a Visualforce Page as PDF from Apex, Click Here.
Final Output β
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
- Find the below steps βΎ
Create Apex Class Controller
Step 1:- Create Apex Class : saveVfPdfCtrl.apxc
From Developer Console >> File >> New >> Apex Class
saveVfPdfCtrl.apxc [Apex Class Controller]
public class saveVfPdfCtrl {
public String MstrId{GET;SET;}
public Account accObj{GET;SET;}
public String PDFNo{GET;SET;}
public String baseURL{GET;SET;}
public PageReference Cancel()
{
PageReference Pdf = NEW PageReference('/'+MstrID);
pdf.setredirect(TRUE);
RETURN Pdf;
}
public saveVfPdfCtrl(ApexPages.StandardController Controller){
baseURL = URL.getSalesforceBaseUrl().toExternalForm();
MstrId = ApexPages.currentPage().getParameters().get('id');
accObj = [SELECT Id, Name, Phone, Industry, Rating, Description, Website, TYPE, (SELECT Id, Name, FirstName, LastName, Email, AccountId, Phone, Title FROM Contacts) FROM Account WHERE Id =: MstrId ];
}
public PageReference pdfAction()
{
PageReference savepage ;
savepage = Page.savePdfVp;
savepage.getParameters().put('id',MstrID);
system.debug('id:- '+MstrID);
BLOB pdfBlob;
IF (!Test.isRunningTest()) {
pdfBlob = savepage.getContent(); //generate the pdf BLOB
} ELSE {
pdfBlob = BLOB.valueOf('Test');
}
List<ContentDocumentLink> notesattch = [SELECT id, ContentDocument.Title,LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId =: MstrID ORDER BY ContentDocument.Title ASC];
system.debug('notesattch## ' + notesattch);
IF(notesattch.size() > 0)
{
string title = notesattch[0].ContentDocument.Title;
system.debug('title111 ' + title);
List<String> titleSplit = title.split('R');
//String FinalTitle = titleSplit[0]+'R0'+notesattch.size();
String FinalTitle = 'PO'+notesattch.size();
system.debug('FinalTitle22 ' + FinalTitle);
PDFNo=FinalTitle;
ContentVersion conVer = NEW ContentVersion();
conVer.ContentLocation = 'S'; // TO USE S specify this document IS IN Salesforce, TO USE E FOR external files
conVer.PathOnClient = FinalTitle+'.pdf';
conVer.Title = FinalTitle;
conVer.VersionData = pdfBlob;
system.debug('conVer@@ ' + conVer);
INSERT conVer;
Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;
ContentDocumentLink conDocLink = NEW ContentDocumentLink();
conDocLink.LinkedEntityId = MstrID;
conDocLink.ContentDocumentId = conDoc;
conDocLink.shareType = 'V';
INSERT conDocLink;
UPDATE accObj;
PageReference pageRef = NEW PageReference( baseURL+'/lightning/r/Account/' + System.currentPageReference().getParameters().get('id')+'/view');
pageRef.setRedirect(TRUE);
//system.debug('pageRef@@@ ' + pageRef);
RETURN pageRef;
}
ELSE{RETURN NULL;}
}
}
Create Apex Test Class Controller
Step 2:- Create Apex Class : saveVfPdfCtrlTest.apxc
From Developer Console >> File >> New >> Apex Class
saveVfPdfCtrlTest.apxc [Apex Class Controller]
@isTest
public class saveVfPdfCtrlTest {
static testmethod void validateStandardController(){
Account acc = NEW Account();
acc.Name='test';
INSERT acc;
Contact con = NEW Contact();
con.FirstName='test';
con.LastName='test';
con.AccountId=acc.Id;
INSERT con;
ContentVersion content=NEW ContentVersion();
content.Title='Header_Picture1';
content.PathOnClient='/' + content.Title + '.pdf';
BLOB bodyBlob=BLOB.valueOf('Unit Test ContentVersion Body');
content.VersionData=bodyBlob;
content.origin = 'H';
INSERT content;
ContentDocumentLink contentlink=NEW ContentDocumentLink();
contentlink.LinkedEntityId=acc.id;
contentlink.contentdocumentid=[SELECT contentdocumentid FROM contentversion WHERE id =: content.id].contentdocumentid;
contentlink.ShareType = 'I';
contentlink.Visibility = 'AllUsers';
INSERT contentlink;
ContentVersion cvlist = NEW Contentversion();
cvlist.Title = 'CZDSTOU';
cvlist.PathOnClient = 'test';
cvlist.VersionData = EncodingUtil.base64Decode('Unit Test Attachment Body');
List<ContentVersion> cvl = NEW List<ContentVersion>();
cvl.add(cvlist);
INSERT cvl;
Test.startTest();
ApexPages.Currentpage().getParameters().put('id',acc.Id);
ApexPages.StandardController sc = NEW ApexPages.StandardController(acc);
saveVfPdfCtrl controller1 = NEW saveVfPdfCtrl(sc);
controller1.pdfAction();
controller1.Cancel();
Test.stopTest();
}
}
Further post that would you like to learn in Salesforce
How to save a VF page rendered as PDF to notes and attachments?
Save the Attachment as PDF using Apex Class and Visualforce Page on click button in Salesforce. You can use the PageReference. getContentAsPDF() method in Apex to render a Visualforce page as PDF data. Then use Apex code to convert that PDF data to an email attachment, a document, a Chatter post, and so on.
How do I save a Visual Studio project as a PDF?
You can export a report from Visual Studio Preview tab, WinForms Print Preview, and WPF Print Preview. Expand the drop-down list with export formats and select PDF File. Specify export options in the invoked PDF Export Options dialog and click OK.
Can Salesforce generate a PDF?
Generating PDFs in Salesforce is a powerful feature that can help you create custom reports, invoices, and other documents.
Related Topics | You May Also Like
Our Free Courses β
π Get Free Course β
π Salesforce Administrators π Salesforce Lightning Flow Builder π Salesforce Record Trigger Flow Builder |
π Get Free Course β
π Aura Lightning Framework π Lightning Web Component (LWC) π Rest APIs Integration |