Reusable Download All Files as Zip Quick Action Lightning Web Component | Salesforce LWC Stack ☁️⚡️



 In this blog you will be able to find code snippet of the Download all Files button. For detailed explanation please watch attached video tutorial at the bottom of the page.

downloadZipQuickAction.html

1
2
3
<template>
    
</template>

downloadZip.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { LightningElement, api, track } from 'lwc';
import getFiles from '@salesforce/apex/DownloadZip.getFiles';
import { NavigationMixin } from "lightning/navigation";


export default class DownloadZipQuickAction extends NavigationMixin(LightningElement) {
  
    @api recordId;
    @track fileIds = '';
    @api invoke() {
        console.log('### I am here!');

        getFiles({
            recordId:this.recordId
        })
            .then(result => { 
                let fileList = JSON.parse(JSON.stringify(result));
                if (fileList != '') { 
                    for (let i in fileList) { 
                        this.fileIds += fileList[i] + '/';
                    }
                }
                
                if (this.fileIds.length > 0) { 
                    this.fileIds = this.fileIds.replace(/.$/, "?");
                }

                const config = {
                    type: 'standard__webPage',
                    attributes: {
                        url: '/sfc/servlet.shepherd/version/download/' + this.fileIds
                    }
                };
                this[NavigationMixin.Navigate](config);

            })
            .catch(error => {
                console.log('### Error: ' + JSON.stringify(error));
             })
    }
}

downloadZipQuickAction.js-meta.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>56.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordAction</target>
    </targets>
<targetConfigs>
  <targetConfig targets="lightning__RecordAction">
    <actionType>Action</actionType>
  </targetConfig>
</targetConfigs>
</LightningComponentBundle>

DownloadZip.cls

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public with sharing class DownloadZip {
    @AuraEnabled
    public static List<Id> getFiles(String recordId){
        Set<Id> contentDocIds = new Set<Id>();
        List<Id> contentVersionIds = new List<Id>();

        for(ContentDocumentLink cdl: [SELECT ContentDocumentId FROM ContentDocumentLink
                                        WHERE LinkedEntityId=:recordId]){
                                            contentDocIds.add(cdl.ContentDocumentId);
                                        }
        
        if(contentDocIds.size()>0){
            for(ContentVersion cv: [SELECT Id FROM ContentVersion 
                                    WHERE ContentDocumentId IN:contentDocIds 
                                    AND isLatest=true]){
                                        contentVersionIds.add(cv.Id);
                                    }
            return contentVersionIds;
        }
        else{
            return null;
        }
    }
}

Output


Checkout Complete Video Tutorial Below


 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Happy Coding :)

Post a Comment

2 Comments

  1. If we need that downloaded file name dynamic, what is the approach Bhai, if you suggest that will be a great help for so many people.

    ReplyDelete
  2. Hi Everyone do you know why only work in Sandbox but when I send to PRD show error

    ReplyDelete