Last active
October 16, 2022 10:18
-
-
Save sfboss/ca93db761188dda1545bfcd84e8b1ba2 to your computer and use it in GitHub Desktop.
PDF Save in Apex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static void savePDFFromCommunity(String bidId, String fileName, String visualForcePage) { | |
| try { | |
| List<Bid__c> bids = [ | |
| SELECT | |
| Id, | |
| UUID_For_Landing_Page__c, | |
| Vendor__r.Name, | |
| Housing__r.Id, | |
| City__r.Name, | |
| Housing__r.Name, | |
| Housing__r.City__c, | |
| Housing__r.City__r.Name, | |
| Housing__r.Date_In__c, | |
| Housing__r.Date_Out__c | |
| FROM Bid__c | |
| WHERE Id = :bidid | |
| ]; | |
| if (bids.size() > 0) | |
| bidid = bids[0].UUID_For_Landing_Page__c; | |
| // get formatted date in and date out entries | |
| String dateIn = (!bids.isEmpty() && bids[0].Housing__r.Date_In__c != null | |
| ? Datetime.newInstance( | |
| bids[0].Housing__r.Date_In__c.year(), | |
| bids[0].Housing__r.Date_In__c.month(), | |
| bids[0].Housing__r.Date_In__c.day() | |
| ) | |
| .format('dd MMMMM yyyy') | |
| : ''); | |
| String dateOut = (!bids.isEmpty() && bids[0].Housing__r.Date_Out__c != null | |
| ? Datetime.newInstance( | |
| bids[0].Housing__r.Date_Out__c.year(), | |
| bids[0].Housing__r.Date_Out__c.month(), | |
| bids[0].Housing__r.Date_Out__c.day() | |
| ) | |
| .format('dd MMMMM yyyy') | |
| : ''); | |
| // create the filename with values from city and formatted dates | |
| // String fileName = bids[0].Vendor__r.Name; | |
| String theFileName = fileName; | |
| fileName = fileName.replaceAll(' ', '_'); | |
| Blob bidAttachment; | |
| String theURL = ''; | |
| String base = [SELECT Id, Community_Base_Url__c FROM RoadRebelSettings__c LIMIT 1].Community_Base_Url__c; | |
| theURL = base + '/' + visualForcePage; | |
| system.debug (theURL); | |
| PageReference pageRef = new PageReference(theURL); | |
| pageRef.getParameters().put('bidid', bidId); | |
| pageRef.getParameters().put('gen', '1'); | |
| if (!Test.isRunningTest()) | |
| bidAttachment = pageRef.getContentAsPDF(); | |
| else | |
| bidAttachment = Blob.valueof('bidAttachmentTest'); | |
| List<ContentDocumentLink> bidDocumentLinks = [ | |
| SELECT id, ContentDocumentId | |
| FROM ContentDocumentLink | |
| WHERE LinkedEntityId = :bids[0].Id AND ContentDocument.title = :fileName | |
| ]; | |
| ContentVersion bidContentVersion = new ContentVersion(); | |
| if (!bidDocumentLinks.isEmpty()) | |
| bidContentVersion.contentDocumentId = bidDocumentLinks[0].ContentDocumentId; | |
| bidContentVersion.PathOnClient = '/' + fileName + '.pdf'; | |
| bidContentVersion.Title = fileName; | |
| bidContentVersion.VersionData = bidAttachment; | |
| if (!Test.isRunningTest()) bidContentVersion.FirstPublishLocationId = bids[0].Id; | |
| if (!Test.isRunningTest()) upsert bidContentVersion; | |
| //else insert bidContentVersion; | |
| // no links exist, no need to worry about new version | |
| // THIS IS SHARING AREA WITH SHARING RULES DETAILS | |
| ContentDocumentLink contentDocumentLink = new ContentDocumentLink(); | |
| if (!bidDocumentLinks.isEmpty()) | |
| contentDocumentLink.ContentDocumentId = [ | |
| SELECT ContentDocumentId | |
| FROM ContentVersion | |
| WHERE Id = :[SELECT Id FROM ContentDocument].Id | |
| ] | |
| .ContentDocumentId; | |
| contentDocumentLink.LinkedEntityId = bids[0].Id; | |
| contentDocumentLink.ShareType = 'V'; | |
| contentDocumentLink.Visibility = 'AllUsers'; | |
| if (!Test.isRunningTest()) insert contentDocumentLink; | |
| } catch (Exception e) { | |
| system.debug( | |
| e.getStackTraceString() + | |
| e.getMessage() | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment