Upload Multiple Files in Lightning Component Without Record Id
Download Notes & Attachments from Force.com Site of Lightning Component
Create an email warning that will be sent to the Opportunity Owner. This email will be when the opportunity is closed won. E-mail Template :
Hi [Owner Proper noun],
The [Opportunity Name] is now closed-won. You can the attachments from the following Link :
[Site Link]
Thanks.
Salesforce Automation
The Site link volition be an open VF folio including (Lightning component). This component will show all the files fastened to the opportunity record in a tabular array format. Also, the user will be able to download these attachments.
[Table]
[FileName1] [FileSize] [Download Button]
[FileName2] [FileSize] [Download Button]
[Tabular array]
Don't forget to bank check out: Salesforce lightning:treegrid - The Developer Guide
Solution:
Step 1: Create a workflow which sends an e-mail alert with text template (binding opp owner Name, Opp Name and site link which concatenate with opp.id) to opportunity possessor on the given criteria whose opportunity phase is closeWon
Email template body:
Hi {!Opportunity.OwnerFullName},
The {!Opportunity. Name } is now closed-won. You lot tin the attachments from the following Link :
http://flowdisplay-programmer-edition.ap16.force.com/EmbedComponentsForSites?id={!Opportunity. Id}
Thanks.
Salesforce Automation
> Opportunity owners receive this email template/body and click on given siteLink (with their particular opportunity id) and they get a lightning component which embeds in the Visualforce folio.
Stride 2:Create a Visualforce page which get the particular opportunity id from the URL of siteLink and passes this id in the lightning component to bear witness the relative notes and attachment on this opportunity in the table
GetIdFromUrl.apxc
public class GetIdFromUrl { public String currentRecordId {get;prepare;} public Cord parameterValue {get;set;} // public Opportunity opp{get;set public GetIdFromUrl(ApexPages.StandardController controller) { currentRecordId = ApexPages.CurrentPage().getparameters().go('id'); system.debug('currentRecordId======='+currentRecordId); //acc = [select id ,proper noun, AccountNumber, Type, Industry from Business relationship where id =: currentRecordId ]; parameterValue = ApexPages.CurrentPage().getparameters().go('nameParam')
> currentRecordId : In GetIdFromUrl.apxc form is used to get the opportunity id from the URL
Visualforce Page: EmbedComponentsForSites.vfp
<apex:page standardController="Opportunity" extensions="GetIdFromUrl" showQuickActionVfHeader="false" showHeader="false" sidebar="false"> <apex:pageBlock > <apex:pageBlockSection championship="Current account record Id is : {!currentRecordId}" collapsible="false"> </noon:pageBlockSection> </apex:pageBlock> <apex:includeLightning /> <div id="FlipcardContainer" /> <script> $Lightning.employ("c:ContactListApp", office() { $Lightning.createComponent("c:FileAttachmentComponent", { recordId : '{!currentRecordId}' }, "FlipcardContainer", office(cmp) { console.log('Component created, do something cool hither'); }); }); </script> </noon:folio>
> recordId : '{!currentRecordId} ': In above visualforce page currentRecordId (opportunity id call back from URL) is passed to the lightning component: FileAttachmentComponent which retrieve their related notes and attachments of the opportunity in the tabular array with required details.
Step 3: Create a tabular array in the Lightning Component which includes fileName, fileSize and Download push
Lightning component: FileAttachmentComponent.cmp
<aura:component implements="forceCommunity:availableForAllPageTypes" access="global" controller="FileAttachment"> <!--aura doInit handler--> <aura:handler proper noun="init" value="{!this}" activeness="{!c.doInit}"/> <!--aura attributes--> <aura:attribute name="Baseurl" type="String" /> <aura:attribute proper name="recordId" type="Id" /> <aura:attribute name="selectedDocumentId" type="cord"/> <aureola:attribute name="lstContentDoc" blazon="List"/> <aureola:aspect proper name="hasModalOpen" type="boolean" default="false"/> id===={!v.recordId} <!-- Custom DataTable to Display List Of Available ContentDocuments Starting time--> <table class="slds-table slds-table_cell-buffer slds-table_bordered"> <thead> <tr class="slds-line-height_reset"> <th grade="slds-text-title_caps" scope="col"> <div form="slds-truncate" title="Championship">Championship</div> </th> <th class="slds-text-title_caps" scope="col"> <div form="slds-truncate" title="size">size(bytes)</div> </th> </tr> </thead> <tbody> <aureola:iteration items="{!v.lstContentDoc}" var="CD"> <tr> <th scope="row"> <div class="slds-truncate" championship="{!CD.Proper name}"> <a onclick="{!c.getSelected}" data-Id="{!CD.Id}">{!CD.Proper name}</a> </div> </thursday> <th scope="row"> <div class="slds-truncate" title="{!CD.BodyLength}"> <a onclick="{!c.getSelected}" information-Id="{!CD.Id}">{!CD.BodyLength}</a> </div> </th> <th scope="row"> <div course="slds-truncate" title="{!CD.ParentId}">{!CD.ParentId}</div> </th> <th telescopic="row"> <!-- <a href="{!'/sfc/servlet.shepherd/document/download/'+CD.Id}" target="_blank" download="{!CD.Name}">Download</a> <a href="{!'/servlet/servlet.FileDownload?file='+CD.Id}" target="_blank">Download</a> --> <a href="{!v.Baseurl}" data-id="{!CD.Id}" onclick = "{!c.downloadfile}" >Download</a> </th> </tr> </aura:iteration> </tbody> </table> <!-- Custom DataTable to Display Listing Of Available ContentDocuments End--> <!--###### FILE PREVIEW MODAL BOX START ######--> <aureola:if isTrue="{!5.hasModalOpen}"> <section onclick="{!c.closeModel}" role="dialog" aria-modal="true" class="slds-modal slds-fade-in-open"> <div class="slds-modal__container"> <div class="slds-modal__content slds-p-around_medium slds-text-align_center" way="groundwork: transparent;"> <div style="width: fifty%; margin: 0 auto; text-align: left"> <!--<lightning:fileCard> to preview file using content certificate Id --> <lightning:fileCard fileId="{!v.selectedDocumentId}"/> </div> </div> </div> </section> <div form="slds-backdrop slds-backdrop_open"></div> </aura:if> <!--###### FILE PREVIEW MODAL BOX END ######--> </aura:component>
Check out another amazing web log past Aditya here: Ship .csv File to Some other Org in Salesforce
FileAttachmentComponentController.js :
({ /*call apex controller method "fetchContentDocument" to get salesforce file records*/ doInit : function(component, event, helper) { var recordId = component.get("5.recordId"); var action = component.get("c.fetchContentDocument"); panel.log('recordid####:'+recordId); action.setParams({ "recordId":recordId }); console.log('recordid1####:'+recordId); action.setCallback(this, function(response) { var state = response.getState(); console.log('state======'+response.getState()); if (country === "SUCCESS") { panel.log('data======1'+response.getReturnValue()); component.ready('v.lstContentDoc', response.getReturnValue()); } else if (country === "INCOMPLETE") { // do something } else if (state === "ERROR") { var errors = response.getError(); if (errors) { if (errors[0] && errors[0].message) { console.log("Error message: " + errors[0].bulletin); } } else { console.log("Unknown error"); } } }); $A.enqueueAction(activity); }, // to download the file downloadfile : function (component, event, helper){ var id = event.target.getAttribute("data-id"); console.log('id===='+id); var action = component.go("c.DownloadAttachment"); console.log('actiondownload####:'+action); action.setParams({ "recordId" : id }); console.log('id==2256=='+id); action.setCallback(this, function(b){ component.ready("v.Baseurl", b.getReturnValue()); console.log('URLLLLL'+b.getReturnValue()); }) $A.enqueueAction(activity); }, // display modle and set up seletedDocumentId attribute with selected record Id getSelected : office(component,event,helper){ component.set("v.hasModalOpen" , truthful); component.gear up("v.selectedDocumentId" , outcome.currentTarget.getAttribute("data-Id")); }, closeModel: function(component, event, helper) { // for Shut Model, set the "hasModalOpen" attribute to "FALSE" component.set("five.hasModalOpen", imitation); component.set("v.selectedDocumentId" , null); }, })
FileAttachment.apxc :
public course FileAttachment { @AuraEnabled public static List<ContentVersion> fetchContentDocument(Id recordId){ system.debug('recordId=='+recordId); //List<Zipper> attachment = [SELECT id, Name, ParentId FROM Zipper Where ParentId =: recordId]; Set<Id> cdIds = new Set<Id>(); /*ContentDocumentLink cdl = new ContentDocumentLink(); cdl.ContentDocumentId = '0062w000002NO8AAAW'; cdl.LinkedEntityId = recordId; cdl.ShareType = 'V'; cdl.Visibility = 'AllUsers'; insert cdl;*/ for(ContentDocumentLink cl : [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId = :recordId]){ cdIds.add(cl.ContentDocumentId); arrangement.debug('cl=='+cl); system.debug('cdIds=='+cdIds); } //list<ContentDocument> attachment = new listing<ContentDocument>([SELECT id, Title, ContentSize FROM ContentDocument Where Id In :cdIds]); list<ContentVersion> attachment = new list<ContentVersion>([SELECT id FROM ContentVersion Where ContentDocumentId In :cdIds]); system.debug('attachment===='+zipper); system.debug('attachMent'+attachMent); render attachMent; } @AuraEnabled public static string DownloadAttachment(Cord recordId) { organisation.debug('recordId====in'+recordId); ContentVersion oldCV = [Select Id,Title from ContentVersion Where ContentDocumentId=:recordId and IsLatest = true ]; Organisation.Debug('Old LV INitial :'+ oldCV.Id); String fullFileURL = URL.getSalesforceBaseUrl().toExternalForm(); String URlDownload = fullFileURL+'/sfc/servlet.shepherd/version/download/'+oldCV.Id; system.debug('Download URL:' +URlDownload); return URlDownload; } } /* * Set up<Id> cdIds = new Set<Id>(); for(ContentDocumentLink cl : [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId =: recordId]){ cdIds.add(cl.ContentDocumentId); system.debug('cl=='+cl); arrangement.debug('cdIds=='+cdIds); } //list<ContentDocument> attachment = new listing<ContentDocument>([SELECT id, Title, ContentSize FROM ContentDocument Where Id In :cdIds]); list<ContentDocument> attachment = new list<ContentDocument>([SELECT id, Title, ContentSize FROM ContentDocument Where ContentDocumentId In :cdIds]); system.debug('zipper===='+zipper); */
> Opportunity owner got id from the url and passed it equally a parameter in the backend(apex command) to recall contentDocument to utilize this id in select query. The apex controller method fetchContentDocument gets the notes and attachments of the opportunity and shows it in the tabular array of lightning components.
> In the table of lightning components there are multiple notes and attachments with fileName, fileSize and Download button.
> When opportunity owner click on Download button, they got the id of that particular file and passes it to backend method DownloadAttachment to utilize this id in Select query to recollect the content version id (fileId) and concatenate it with baseURL:
String URlDownload = fullFileURL+'/sfc/servlet.shepherd/version/download/'+oldCV.Id;
Where fullFileURL is baseURL.
Reference: sfdcmonkey
Source: https://www.forcetalks.com/blog/files-attachments-and-notes-salesforce-lightning-components-force-com/