salesforce中的添付文件的增删改查

salesforce中的添付文件涉及到三个对象

如果想删除某个object底下的添付文件的时候

```
第一步，ContentDocumentLink中以LinkedEntityId=关联obj id为条件取得ContentDocumentId字段的值
第二步，按照ContentDocumentId删除ContentDocument
只要删除ContentDocument，对应的ContentDocumentLink和ContentVersion都会自动删除

list<ContentDocument> lstCntDocsToDelete = new list<ContentDocument>();

for(ContentDocumentLink iterator : [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId =:idLinkedEntity]) {
    lstCntDocsToDelete.add(new ContentDocument(Id = iterator.ContentDocumentId));
}

if(!lstCntDocsToDelete.isEmpty() && lstCntDocsToDelete != null) {
    Database.delete(lstCntDocsToDelete, false);
    Database.emptyRecycleBin(lstCntDocsToDelete);
}

```

如果想手动登陆某个object底下的添付文件的时候

```
1.创建一个ContentVersion，将blob格式的文件内容储存到VersionData 
2.创建一个ContentDocumentLink，指定关联的obj，使用link.LinkedEntityId = 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;
    }


```

如果获取某个object底下的添付文件内容的时候

```
1首先，通过objID获取到ContentDocumentLink
2然后，通过ContentDocumentId 获取ContentVersion
3返回VersionData即可




  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;
        }
    }

```



```
加入你想追加一自定义可以下载的link


 Set<Id> attIdList = new Set<Id>();
                    for(你的objectList att : attList){
                        attIdList.add(att.id);
                    }
                    List<ContentDocumentLink> links = [select LinkedEntityId,ContentDocumentId, Id 
                                                                                from ContentDocumentLink 
                                                                                where LinkedEntityId in :attIdList ];

                    Set<Id> linkIdList = new Set<Id>();
                    for(ContentDocumentLink link : links){
                        linkIdList.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 :linkIdList
                                                ];

                    Map<String, ContentVersion> relationMap = new Map<String, ContentVersion>();
                    for(ContentDocumentLink link : links){
                        for(ContentVersion con : conVer){
                                if(link.ContentDocumentId==con.ContentDocumentId){
                                    relationMap.put(link.LinkedEntityId,con);
                                }
                        }
                    }
                                            


                   
                    for(你的objectList att : attList){

                        if(relationMap.containsKey(att.id)){
                            file.FileName = '<a href="/sfc/servlet.shepherd/document/download/'+relationMap.get(att.id).ContentDocumentId+'?operationContext=S1"  style="text-decoration:underline;">'
                                            +relationMap.get(att.id).Title + '.'+relationMap.get(att.id).FileType +'<a/>';
                        }

                        filelist.add(file);
                    }
                    

```















https://salesforce.oikeru.com/entry/ContentDocument_ContentLink_ContentVersion