LWC Quick Action: How to Download Records as Zip File in Minutes

By | December 13, 2023

In this blog post will walk you through the process of effortlessly downloading (Download Records as Zip File) connected records for all attachments with just one click. Utilizing a Headless Quick Action approach enables you to easily download records related file and all linked attachments as a zip file.

To achieve this, we will be using a headless quick-action approach.

Download Records as Zip File

  • Refer to the following code for implementation on the “Download Files” button that will be used for download records as zip file:
downloadAllAsZipQuickActionbutton.html
<template>
</template>
downloadAllAsZipQuickActionbutton.js
import { LightningElement, api, track } from 'lwc';
import getAttachement from '@salesforce/apex/DownloadAllAttacmentsZip.getAttachement';
import { NavigationMixin } from "lightning/navigation";

export default class DownloadZipQuickAction extends NavigationMixin(LightningElement) {
  
    @api recordId;
    @track AttachementIds = '';

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

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

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

downloadAllAsZipQuickActionbutton.cls
<?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>

DownloadAllAttacmentsZip.cls

public with sharing class DownloadAllAttacmentsZip 
{
    @AuraEnabled
    public static List<Id> getAttachement(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;
        }
    }
}
Download files Quick Action
Download files Quick Action
  • Output:
Download Records as Zip File
Downloaded files

By following the above blog instructions, you will be able to learn “How to Download Records as Zip File using the quick action button in the lightning web component?“. If you still have queries or any related problems, don’t hesitate to contact us at salesforce@greytrix.com.

More details about the Product are available on our website and Salesforce AppExchange.

We hope you may find this blog resourceful and helpful. However, if you still have concerns and need more help, please contact us at salesforce@greytrix.com.

References:

Related Posts