3

I uploaded jpeg image for an account. The jpeg image file id is 069i0000001dkl8 and it can't access via, https://c.na15.content.force.com/servlet/servlet.FileDownload?file=069i0000001dkl8

But it can acces via, https://c.na15.content.force.com/sfc/servlet.shepherd/version/download/068i0000001hwPn?asPdf=false&operationContext=CHATTER

Is there a way that I can get downloadable URL for attachment in salesforce (using api calls)? Or Is there a way that I can build downloadable URL by processing some fields in API object (SObject)?

Thanks.

lsc
  • 681
  • 3
  • 8
  • 26

2 Answers2

5

In winter 15 Salesforce made this possible. You can create a class that converts an attachment to ContentVersion and ContentDistribution. Then pass the user the DistributionPublicUrl field of ContentDistribution.

Code will be something like this

            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 ;
Amr Ibrahim
  • 151
  • 1
  • 4
4

Technically speaking, you are dealing with ContentDocument (069 key prefix) and ContentVersion (068 key prefix) records rather than an Attachment (00P key prefix).

Have a look at the Data Model for Content Objects:

Content Data Model

You can use SOQL to create a query that will get you the correct ContentVersion for a ContentDocument. The resulting ID can then be used to create the download URL.

Alternatively, you could get the binary contents of the attachment from the ContentVersion directly via the API.

Incidentally, the Salesforce Stackexchange site is a great place to ask Salesforce specific questions.

Daniel Ballinger
  • 13,187
  • 11
  • 69
  • 96
  • Thanks for the very clear reply. Also is there a way to get attachment id (00P key prefix) when we have ContentDocument (069 key prefix) or ContentVersion (068 key prefix) ids? – lsc Oct 31 '14 at 08:21
  • @lasithc Attachments and Content are two separate records types. You can't directly move between them. I.E. these is no attachment record with the same data as is stored in the ContentVersion. Both Attachment and ContentVersion store binary data. – Daniel Ballinger Nov 01 '14 at 21:32