Memos About Salesforce

Salesforceにハマってたこと!

Salesforce 添付ファイル ContentDocument ContentLink ContentVersion

こんにちは、管理人の@Salesforce.Zです。

Salesforceは進化している、そして、早い。

従来からの添付ファイルはサブクエリー(SELECT Id, Name FROM Attachments)を使って取得できる。

しかし、今後はFilesに格納されたファイルは ContentDocumentLinkオブジェクトから取得するになる。

今回、添付ファイルについて、なぞなぞを解明していきたいと思います。

目次

添付ファイル

ファイル本体

ファイル本体はContentVersion#VersionDataフィールドにBase64形式で格納されている。

ContentDocument

ContentDocumentはファイルを管理する親オブジェクトである

クエリ例

//LinkedEntityIdにレコードのIDを指定すると、そのレコードに紐付くファイルの一覧を取得できる
ContentDocumentLink conLink = [SELECT ContentDocumentId, Id FROM ContentDocumentLink WHERE LinkedEntityId = :対象レコードID];

//上記で取得できるのはContentDocumentIdなので、実際にダウンロード可能な各バージョンは
 ContentVersionオブジェクトから取得する
ContentVersion conVer = [SELECT Id
                                            , CreatedDate
                                            , Title
                                            , Description
                                            , FileType
                                            , Owner.UserName
                                            , VersionNumber
                                            , ContentDocumentId 
                                    FROM ContentVersion Where IsLatest = true
                                    AND ContentDocumentId = :conLink.ContentDocumentId
                                    ORDER BY CreatedDate desc];

対象レコードに添付ファイルを添付する

public with sharing class UtilFile{
    
    public static void attachFile(String file ,String title , Id objId){
        attachFile(Blob.valueOf(file) , title, objId); 
    }

    //ファイルを指定されたレコードに添付する
    public static void attachFile(Blob file ,String title , Id objId){
        
        ContentVersion cv = new ContentVersion();
        cv.Title          = title;
        cv.VersionData    = file;
        cv.IsMajorVersion = true;
        cv.PathOnClient   = 'content txt';
        insert cv;

        cv = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id];

        ContentDocumentLink link = new ContentDocumentLink();
        link.LinkedEntityId = objId;
        link.ContentDocumentId = cv.ContentDocumentId;
        //権限
        link.ShareType = 'V';

        insert link;
    }

    /**
    * base64形式でファイルを取得
    */ 
    public static Blob getFile(Id objId , String fileName){
        
        
        List<ContentDocumentLink> links = [select ContentDocumentId, Id 
                            from ContentDocumentLink 
                            where LinkedEntityId = :objId ];
        Set<Id> recordIdList = new Set<Id>();
        for(ContentDocumentLink link : links){
            recordIdList.add(link.ContentDocumentId);
        }


        //最新ファイルバージョンのみ取得する
        List<ContentVersion> conVer = [select Id, 
                                            CreatedDate, 
                                            Title,
                                            Description,
                                            FileType, 
                                            Owner.UserName, 
                                            VersionNumber,
                                            VersionData,
                                            ContentDocumentId 
                                    from ContentVersion 
                                    where IsLatest = true 
                                        and ContentDocumentId in :recordIdList
                                        and Title = :fileName
                                    order by CreatedDate desc
                                    limit 1];

        if(!conVer.isEmpty()){
            return conVer[0].VersionData;
        }else{
            return null;
        }
    }
}