lightning component

Files, Attachments And Notes Salesforce Lightning Components |

Download Notes & Attachments from Site of Lightning Component

Create an email alert that will be sent to the Opportunity Owner. This email will be when the opportunity is closed won. Email Template : 

Hi [Owner Name],

The [Opportunity Name] is now closed-won. You can the attachments from the following Link :

[Site Link]


Salesforce Automation

The Site link will be an open VF page including (Lightning component). This component will show all the files attached to the opportunity record in a table format. Also, the user will be able to download these attachments.


[FileName1] [FileSize] [Download Button]

[FileName2] [FileSize] [Download Button]


dont miss out iconDon't forget to check out: Salesforce lightning:treegrid - The Developer Guide


Step 1: Create a workflow which sends an email alert with text template (binding opp owner Name, Opp Name and site link which concatenate with to opportunity owner on the given criteria whose opportunity stage is closeWon

Email template body: 

Hi {!Opportunity.OwnerFullName},

The {!Opportunity. Name} is now closed-won. You can the attachments from the following Link :{!Opportunity. Id}


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 page.

Step 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 show the relative notes and attachment on this opportunity in the table


public class GetIdFromUrl {
    public String currentRecordId {get;set;}
    public String parameterValue {get;set;}
    // public Opportunity opp{get;set 
    public GetIdFromUrl(ApexPages.StandardController controller) {
        currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        //acc = [select id ,name, AccountNumber, Type, Industry from Account where id =: currentRecordId ];
        parameterValue = ApexPages.CurrentPage().getparameters().get('nameParam')

>currentRecordId : In GetIdFromUrl.apxc class 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 title="Current account record Id is : {!currentRecordId}" collapsible="false">
    <apex:includeLightning />    
    <div id="FlipcardContainer" />    
    $Lightning.use("c:ContactListApp", function() {
                                   {  recordId : '{!currentRecordId}'  },
                                   function(cmp) {
                                       console.log('Component created, do something cool here');

>  recordId : '{!currentRecordId}': In above visualforce page currentRecordId (opportunity id retrieve from URL) is passed to the lightning component:FileAttachmentComponent which retrieve their related notes and attachments of the opportunity in the table with required details.

Step 3: Create a table in the Lightning Component which includes fileName, fileSize and Download button 

Lightning component: FileAttachmentComponent.cmp

<aura:component implements="forceCommunity:availableForAllPageTypes" access="global" 
    <!--aura doInit handler--> 
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>    
    <!--aura attributes--> 
    <aura:attribute name="Baseurl" type="String" />
    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="selectedDocumentId" type="string"/>
    <aura:attribute name="lstContentDoc" type="List"/>
    <aura:attribute name="hasModalOpen" type="boolean" default="false"/>
    <!-- Custom DataTable to Display List Of Available ContentDocuments Start-->  
    <table class="slds-table slds-table_cell-buffer slds-table_bordered">
            <tr class="slds-line-height_reset">
                <th class="slds-text-title_caps" scope="col">
                    <div class="slds-truncate" title="Title">Title</div>
                <th class="slds-text-title_caps" scope="col">
                    <div class="slds-truncate" title="size">size(bytes)</div>
            <aura:iteration items="{!v.lstContentDoc}" var="CD">
                    <th scope="row">
                        <div class="slds-truncate" title="{!CD.Name}">
                            <a onclick="{!c.getSelected}" data-Id="{!CD.Id}">{!CD.Name}</a>
                    <th scope="row">
                        <div class="slds-truncate" title="{!CD.BodyLength}">
                            <a onclick="{!c.getSelected}" data-Id="{!CD.Id}">{!CD.BodyLength}</a>
                    <th scope="row">
                        <div class="slds-truncate" title="{!CD.ParentId}">{!CD.ParentId}</div>
                    <th scope="row">
                        <!-- <a href="{!'/sfc/servlet.shepherd/document/download/'+CD.Id}" target="_blank" 
                       <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>
    <!-- Custom DataTable to Display List Of Available ContentDocuments End-->  
    <!--###### FILE PREVIEW MODAL BOX START ######--> 
    <aura:if isTrue="{!v.hasModalOpen}">
        <section onclick="{!c.closeModel}"
                 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"
                     style="background: transparent;">
                    <div style="width: 50%; margin: 0 auto; text-align: left">
                        <!--<lightning:fileCard> to preview file using content document Id -->
                        <lightning:fileCard fileId="{!v.selectedDocumentId}"/>
        <div class="slds-backdrop slds-backdrop_open"></div>
    <!--###### FILE PREVIEW MODAL BOX END ######-->  

dont miss out iconCheck out another amazing blog by Aditya here: Send .csv File to Another Org in Salesforce

FileAttachmentComponentController.js :

    /*call apex controller method "fetchContentDocument" to get salesforce file records*/
    doInit : function(component, event, helper) {
        var recordId = component.get("v.recordId");
        var action = component.get("c.fetchContentDocument");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set('v.lstContentDoc', response.getReturnValue());
            else if (state === "INCOMPLETE") {
                // do something
                else if (state === "ERROR") {
                    var errors = response.getError();
                    if (errors) {
                        if (errors[0] && errors[0].message) {
                            console.log("Error message: " + 
                    } else {
                        console.log("Unknown error");
    // to download the file
    downloadfile : function (component, event, helper){
        var id ="data-id");       
        var action = component.get("c.DownloadAttachment");
        action.setParams({ "recordId" :  id });
        action.setCallback(this, function(b){
            component.set("v.Baseurl", b.getReturnValue());
    // display modle and set seletedDocumentId attribute with selected record Id   
    getSelected : function(component,event,helper){       
        component.set("v.hasModalOpen" , true);
        component.set("v.selectedDocumentId" , event.currentTarget.getAttribute("data-Id"));        
    closeModel: function(component, event, helper) {
        // for Close Model, set the "hasModalOpen" attribute to "FALSE"  
        component.set("v.hasModalOpen", false);
        component.set("v.selectedDocumentId" , null); 

FileAttachment.apxc :

public class FileAttachment {
    public static List<ContentVersion> fetchContentDocument(Id recordId){
        //List<Attachment> attachMent =  [SELECT  id, Name, ParentId FROM Attachment 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]){
        //list<ContentDocument> attachMent = new list<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]); 
        return attachMent;
    public static string DownloadAttachment(String recordId)
        ContentVersion oldCV = [Select Id,Title from ContentVersion Where ContentDocumentId=:recordId and IsLatest = true ];
        System.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<Id> cdIds = new Set<Id>();
for(ContentDocumentLink  cl : [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId =: recordId]){
//list<ContentDocument> attachMent = new list<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]); 

> Opportunity owner got id from the url and passed it as a parameter in the backend(apex control) to retrieve contentDocument to use this id in select query. The apex controller method fetchContentDocument gets the notes and attachments of the opportunity and shows it in the table 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 use this id in Select query to retrieve 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

Popular Salesforce Blogs