1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Salesforce] レコードに添付したファイルを一括でダウンロードする

Last updated at Posted at 2025-01-22

この記事では「添付ファイル」はAttachmentではなく、ContentVersionのことを指しています。

Salesforce上のファイルをダウンロードするURLについて

この記事で紹介するサーブレットURLは公式にサポートされているものではなく、予告なく変更される可能性があります。

Salesforce上のファイルをダウンロードするURLが存在しています。それが下記の2つです。

  • /sfc/servlet.shepherd/version/download/{ContentVersionId}
  • /sfc/servlet.shepherd/document/download/{ContentDocumentId}

idをスラッシュで区切って複数渡すことで複数ファイルまとめてZipファイルでダウンロードできます。

  • /sfc/servlet.shepherd/version/download/068XXXXXXXXXXXXXXX/068YYYYYYYYYYYYYYY
  • /sfc/servlet.shepherd/document/download/069XXXXXXXXXXXXXXX/069YYYYYYYYYYYYYYY

これを利用して、レコードに紐づくファイルをLWC、Aura、Visualforceなどで一括でダウンロードする機能が開発可能です。

実装例

レコードに添付されているファイルをZIPファイルで一括ダウンロードするクイックアクションの例です。

image.png

<template>
    <lightning-quick-action-panel header="ファイルダウンローダー">
        <template lwc:if={fileListSize}>
            <p>{fileListSize}個の添付ファイルをダウンロードします。よろしいですか?</p>
            <div slot="footer">
                <lightning-button variant="neutral" label="キャンセル" onclick={handleClickCancel}></lightning-button>
                <lightning-button variant="brand" label="OK" class="slds-m-left_x-small" onclick={handleClickOk}></lightning-button>
            </div>
        </template>
        <template lwc:else>
            <p>添付ファイルが見つかりません。</p>
            <div slot="footer">
                <lightning-button variant="neutral" label="キャンセル" onclick={handleClickCancel}></lightning-button>
            </div>
        </template>
    </lightning-quick-action-panel>
</template>
import { LightningElement, api, wire } from "lwc";
import { NavigationMixin } from "lightning/navigation";
import { CloseActionScreenEvent } from "lightning/actions";
import fetchRelatedFileIdList from "@salesforce/apex/FileDownloaderController.fetchRelatedFileIdList"

const FILES_DOWNLOAD_URL = "/sfc/servlet.shepherd/document/download";

export default class FileDownloader extends NavigationMixin(LightningElement) {
    @api recordId;
    @wire(fetchRelatedFileIdList, { parentId: "$recordId"})
    fileIdList;

    get fileListSize() {
        return this.fileIdList.data?.length;
    }

    handleClickOk() {
        if (this.fileIdList.data) {
            const downloadUrl = this.fileIdList.data.reduce((previous, current) => (previous += `/${current}`), FILES_DOWNLOAD_URL);
            this.navigate(downloadUrl);
        }
    }

    handleClickCancel() {
        this.dispatchEvent(new CloseActionScreenEvent());
    }

    navigate(url) {
        this[NavigationMixin.Navigate]({
            type: "standard__webPage",
            attributes: { url }
        }, true);
    }
}
<?xml version="1.0"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>62.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordAction</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightning__RecordAction">
            <actionType>ScreenAction</actionType>
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>
public with sharing class FileDownloaderController {
    @AuraEnabled
    public static List<Id> fetchRelatedFileIdList(Id parentId) {
        List<Id> documentIds = new List<Id>();
        for (ContentDocumentLink link : [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :parentId]) {
            documentIds.add(link.ContentDocumentId);
        }
        return documentIds;
    }
}

Appendix

PDFファイルでダウンロードする

クエリパラメータにasPdf=trueを追加するとPDFとしてダウンロードされます。

ファイルのプレビューを表示する

/sfc/servlet.shepherd/version/renditionDownload?rendition={種別}&versionId={ContentVersionId}

renditionに使用できる値は下記です。

  • SVGZ
  • ORIGINAL_Jpg
  • ORIGINAL_Png
  • THUMB120BY90 - サムネイル
  • THUMB240BY180 - サムネイル
  • THUMB720BY480 - サムネイル

参考

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?