3
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は公式にサポートされているものではなく、予告なく変更される可能性があります。
ファイル名に日本語が含まれると、ファイル名はSalesforceIDに変換されます。

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などで一括でダウンロードする機能が開発可能です。

※ただし、コンテンツドキュメントのファイル名に日本語が含まれると、ファイル名がSFIDに変換されてダウンロードされてしまいます。

実装例

レコードに添付されているファイルを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 { gql, graphql } from "lightning/uiGraphQLApi";

const FILES_DOWNLOAD_URL = "/sfc/servlet.shepherd/document/download";
const QUERY = gql`
  query GetFiles($recordId: ID!) {
    uiapi {
      query {
        ContentDocumentLink(where: { LinkedEntityId: { eq: $recordId } }) {
          edges {
            node {
              ContentDocument {
                Id
              }
            }
          }
        }
      }
    }
  }
`;

export default class MassFileDownloader extends NavigationMixin(LightningElement) {
  @api recordId;
  files;

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

  @wire(graphql, {
    query: "$recordQuery",
    variables: "$variables"
  })
  queryGraphql(value) {
    this.wiredData = value;
    const { errors, data } = value;
    if (data) {
      const records = data.uiapi?.query?.ContentDocumentLink?.edges?.map((record) => record.node.ContentDocument);
      this.files = records;
    } else if (errors) {
      console.error("GraphQL error", errors);
    }
  }

  get recordQuery() {
    if (!this.recordId) {
      return undefined;
    }
    return QUERY;
  }

  get variables() {
    return {
      recordId: this.recordId
    };
  }

  handleClickOk() {
    if (this.files) {
      const downloadUrl = this.files.reduce((previous, current) => (previous += `/${current.Id}`), 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>

Appendix

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

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

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

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

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

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

プレビューが表示されない場合はこちらを参照してください。

参考

3
2
0

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
3
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?