はじめに
ContentDistributionを使用すると以下のことができました!
- アップロードしたファイルへのアクセスに対してパスワードを付与する
- ファイルに対してURLを発行し、ログインせずにアクセスできる
- URLをメールの文章に置き換えることによって、メールで共有できる
実際に作ってみた
想定シチュエーションはこんな感じ
\(^o^)/はじまり
- ケース(Case)を作成する
- ケースにファイルをアップロードする
- ケースの状況(Status)を[Closed]に変更する
- ケースのトリガが動く(BeforeUpdate)
- 2.でアップロードしたファイル(ContentVersion)を取得する
- ContentDistributionを作成する
- ContentDistributionのContentDownloadUrlを1.で作成したケースの説明(Description)に設定する
- ワークフローにてメールを送信する
\(^o^)/おわり
コードはこんな感じ
CaseTrigger
CaseTrigger.trigger
trigger CaseTrigger on Case(before update) {
if(Trigger.isUpdate && Trigger.isBefore){
CaseTriggerHandler handler = new CaseTriggerHandler();
handler.onBeforeUpdate(Trigger.new, Trigger.newMap, Trigger.old, Trigger.oldMap);
}
}
CaseTriggerHandler
CaseTriggerHandler.cls
public void onBeforeUpdate(Case[] newList, Map<Id, Case> newMap, Case[] oldList, Map<Id, Case> oldMap){
// 処理対象のケースを保持するため、変数を用意する(今回は単一レコードの処理を想定)
Case targetCase = new Case();
// 状況が[Closed]に変更されたケースを取得する
if(newList[0].Status == 'Closed' && oldList[0].Status != 'Closed'){
targetCase = newList[0];
}
// 処理対象のケースが存在する場合、紐づいているファイルを取得する
ContentVersion targetCV = new ContentVersion();
if(targetCase.Id != NULL){
targetCV = [SELECT Id, Title, VersionData
FROM ContentVersion
WHERE FirstPublishLocationId = :targetCase.Id
ORDER BY CreatedDate Desc
LIMIT 1];
}
// 処理対象のファイルが存在する場合、ContentDistributionを作成する
if(targetCV.Id != NULL){
ContentDistribution cD = new ContentDistribution();
cD.Name = targetCV.Title;
cD.ContentVersionId = targetCV.Id;
cD.PreferencesPasswordRequired = true;
insert cD;
// URL、パスワードがNULLの状態のため、再取得を行う
cD = [SELECT DistributionPublicUrl, Password FROM ContentDistribution WHERE Id = :cD.Id];
targetCase.Description = cD.DistributionPublicUrl;
targetCase.Description += '\r\n';
targetCase.Description += cD.Password;
}
}
動作時のキャプチャ
1.ケース(Case)を作成する












こんなことはできないのかなあ?
- アップロードしたファイル自体にパスワードを付与する
- パスワードの桁数や設定文字を開発者が決める
などなど・・・
参考リンク
SOAP API 開発者ガイド:ContentVersion
SOAP API 開発者ガイド:ContentDistribution
ワークフローで久しぶりにメール送った
こんなもんで あぁ~~イイッすかねェェェェェ~~と