0
0

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-02

標準機能では設定できそうでできない、そんな設定を Apex を用いて設定する方法を紹介していきたいと思います。
Apexを書いたことがない人でも、コピー&ペーストで、基本的に設定が完了します!

本記事では、「ファイルをアップロードしたときに、そのファイルを特定のユーザー(あるいは全てのユーザー)に対して自動で共有させる方法」について紹介します。

前提として・・・

「ファイルをアップロードしたときに、自動で〇〇」という自動化処理設定は、フローでは実現できない可能性が高いです。
なぜなら、現在のレコードトリガーフローでは、ファイル(ContentDocument や ContentVersion)に関するオブジェクトをトリガーのオブジェクトとして指定できないからです。
そのため、Apexを用いた開発を行う方法を紹介します。

スクリーンショット 2025-01-02 19.39.52.png

設定方法

1.ファイルアップロード時に共有したユーザーをメンバーとした Chatter グループを作成

2.ContentDocumentLink レコードを作成するための、Apex クラスとメソッドを作成
→ファイルの共有に関する情報は、ContentDocumentLink オブジェクトに格納されます。そのため、ContentDocumentLink レコードを作成することで、共有設定を実施します。

3.ContentVersion の after insert の Apex トリガーを作成
→ファイルをアップロードすることで、ContentVersion レコードが作成されます。そのため ContentVersion レコードの作成をトリガーとした Apex トリガーを作成します。

 

Chatter グループを作成

Chatterタブの「最近参照したグループ」+ボタンよりグループを作成して、ファイルアップロード時にファイルを共有したいユーザーをメンバーとして追加します。
※標準機能で作成が完了するため、詳細な設定方法は割愛します。

■ご参考:Chatter グループの作成
https://help.salesforce.com/s/articleView?id=sf.collab_group_creating.htm&type=5
 

ContentDocumentLink レコードを作成するための、Apex クラスとメソッドを作成

設定>Apexクラス>新規 にて下記の Apex を記載し、保存します。
0F9xxxxxxxxxxxxxxx の部分には、作成した Chatter グループを開いた際の URL の「0F9」から始まる 18 桁の文字列を記載します。

//filesクラスを作成
public class Files {   
    public void contentdoclink (ID id){
        ContentDocumentLink rec = new ContentDocumentLink(ContentDocumentId = id, LinkedEntityId = '0F9xxxxxxxxxxxxxxx');
        insert rec;
    }
}

■ご参考:ContentDocumentLink
https://developer.salesforce.com/docs/atlas.ja-jp.object_reference.meta/object_reference/sforce_api_objects_contentdocumentlink.htm
 

ContentVersion の after insert の Apex トリガーを作成

歯車マーク>開発者コンソール>New>Apex Trigger をクリック
Apex トリガーの任意の Name を記入し、sObject にて ContentVersion を選択。

下記の Apex を記載し、保存します。(下記では、Apex トリガーの名前を「filekyouyuu」としています)

trigger filekyouyuu on ContentVersion (after insert) {   
    for (ContentVersion record : trigger.new){
        Files createlink = new Files();//filesクラスをインスタンス化
        createlink.contentdoclink(record.ContentDocumentId);//contentdoclinkメソッドを呼び出し
    }
}

■ご参考:ContentVersion
https://developer.salesforce.com/docs/atlas.ja-jp.object_reference.meta/object_reference/sforce_api_objects_contentversion.htm

以上の Apex を設定後、ファイルをアップロードすると、自動で Chatter グループのメンバーに対して閲覧者のアクセス権が付与されると思います。

 

備忘録

ファイルをアップロードした際は、「ContentDocument」レコードも作成されます。
ですが、Apex トリガーにて「ContentDocument」レコードの作成をトリガーとした場合、エラー(INVALID_CROSS_REFERENCE_KEY)が発生して上手く行きませんでした。。
ですが、非同期処理(Queueable インターフェース)を行うことでエラーが回避できたました。

Apex クラス

public class FilesQueueable implements Queueable {

    //コンストラクタ
    public ID contentdocid;
    public FilesQueueable (ID id) {
        contentdocid = id;
    }
    
    public void execute(QueueableContext context){
        ContentDocumentLink rec = new ContentDocumentLink(ContentDocumentId = contentdocid, LinkedEntityId = '0F9xxxxxxxxxxxxxxx');
        insert rec;
    }
}

Apex トリガー

trigger filekyouyuu_hidouki on ContentDocument (after insert) {    
    for (ContentDocument record : trigger.new){
        FilesQueueable  createlink = new FilesQueueable (record.id);
        System.enqueueJob(createlink);
    }
}

■ご参考:Queueable インターフェース
https://developer.salesforce.com/docs/atlas.ja-jp.apexcode.meta/apexcode/apex_class_System_Queueable.htm

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?