如何获取附件的可下载链接?

lsc*_*lsc 3 salesforce salesforce-service-cloud

我为一个帐户上传了 jpeg 图像。jpeg 图像文件 id 是 069i0000001dkl8,它无法通过https://c.na15.content.force.com/servlet/servlet.FileDownload?file=069i0000001dkl8访问

但它可以通过 https://c.na15.content.force.com/sfc/servlet.shepherd/version/download/068i0000001hwPn?asPdf=false&operationContext=CHATTER 访问

有没有办法可以在 Salesforce 中获取附件的可下载 URL(使用 api 调用)?或者有没有办法通过处理 API 对象 (SObject) 中的某些字段来构建可下载的 URL?

谢谢。

小智 5

15 年冬天,Salesforce 使这成为可能。您可以创建一个将附件转换为ContentVersionContentDistribution 的类。然后将 ContentDistribution 的 DistributionPublicUrl 字段传递给用户。

代码将是这样的

            list<Attachment> invoices = [select id, name, body from attachment limit 10];
            list<ContentVersion> contents = new list<ContentVersion>();
            list<ContentDistribution> dists = new list<ContentDistribution>();

            for(Attachment inv: invoices){
                ContentVersion cont = new ContentVersion();
                cont.Title = inv.Name;
                cont.PathOnClient =  inv.Name;
                cont.VersionData = inv.Body;
                contents.add(cont);
            }
            insert contents;

            for(ContentVersion cont : contents){
                ContentDistribution cd = new ContentDistribution();
                cd.name = cont.Title;
                cd.ContentVersionId = cont.id;
                cd.PreferencesAllowOriginalDownload = true;
                cd.PreferencesAllowPDFDownload = true;
                cd.PreferencesAllowViewInBrowser = true;
                dists.add(cd); 
            }             

            insert dists ;
Run Code Online (Sandbox Code Playgroud)