Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

I uploaded a 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 access 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.

1 Answer

0 votes
by (32.1k points)

Yes, recently Salesforce made this possible. For this, you can design a class which converts an attachment to ContentDistribution and ContentVersion. Then you can pass the user the DistributionPublicUrl field of ContentDistribution.

Your code should look 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 ;

Browse Categories

...