function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ricky FairRicky Fair 

Using the REST API to Download documents/Attachments

SalesForce Devs,
 
I am simply trying to invocate a GET request call to download a file/document, previously uploaded to Salesforce, from Salesforce. I am able to successfully authenticate, and perform other calls. It appears that the only way to download the file according to the SFDC documentation, listed in link below, is by providing a document/attachment ID. Which, I've tried determine and find without success, and I have searched throughout the system very thoroughly and have not been able to determine what the document/attachment IDs are for the files that I am attemptign to test download with.

Also, what are the differences between documents and attachements? Should I be using the Files API instead?

 
https://developer.salesforce.com/docs/api-explorer/sobject/Attachment
https://developer.salesforce.com/docs/api-explorer/sobject/Document
 
Thank you for any help insight you are able to provide! There's no way I am the first person to attempt this!
 
RF
 
vijay kumar kvijay kumar k
Hi Ricky

Thank you for posting this question.
As per my knowledge if you give any request the response will give you in string format.So based on ur response you need to do some logic.
In the same SF URLs wt u gave for calling Attachment to just getting id and Success response here YOU won't expect attachment body.and also you hitting standard salesforce Endpoint URL.So it's returning another URL for body.
So my suggestion is create your own apex class having strong endpoint URL with HTTPGET method for returning direct body instead of another URL(as showning in standard response) and this response also will give you in string format only.

if you want to download attachments u can redirect u r url with passing id of record. The download URL is:
https://(DOMAINNAME)--c.ap4.content.force.com/servlet/servlet.FileDownload?file=00P6F00002r55f5(Record ID). 

You can set this link as a redirect page at you created custom URL apex class only (IF you want any help on this ask me I'll send you draft code).So if the apex class is calling the above link the particular file automatically gets downloaded. If it's a success no need to look into response body.

The only difference in attachment and documents is attachments have parentId, so file can be visible at record related list level.

Better check with public links for downloading attachments using public URL no need to login salesforce if clink links the respective file automatically downloads. For generating public URL of attachments is gone through https://help.salesforce.com/articleView?id=collab_files_sharing_via_link.htm&type=5 (https://help.salesforce.com/articleView?id=collab_files_sharing_via_link.htm&type=5)

I hope this is helpfull to you.
Ricky FairRicky Fair
Hi Vijay,

That is very helpful, but I dont think it fully addresses my issue. I will add clarification.

End goal: To be able to download all files from SFDC for a specific user, opportunity, lead, etc. by calling an SFDC API, and providing the Object type (user, opportunity, lead, etc.) and the ID associated with that user, opportunity, lead, etc.

Also, I have not been able to obtain/identfiy the attachment ID nor Document ID. I tried to determin what the document ID is, to attempt to download it from the Attachment/document APIs. I searched the UI, and associated objects that may provide the document/attachment ID.

From your post, it sounds like the document/attachment ID will work with an APEX class that redirect me to a seperate URL for download. How do I first get the attachment/document ID?

After uploading 3 files to a lead I created in the SFDC UI, I am able to make the files public, and download them. I performed an inspection on the networking traffic they produce when downloading, I found certain ID numbers etc. but am unable to confirm they are the ID numbers that I need. Please note the End goal I listed above, is there no simple way to tap into SFDC APIs after authenticating to download releavant attachments to an object?

Thanks again for your help!
Ricky
 
vijay kumar kvijay kumar k
Hi Ricky 

I do agree with you using SFDC API we can't download attachments. That is the reason I gave you go through custom code.
ok fine, can you please describe your business scenario so that anybody can help you including me also on time with a point to point explanation.

Regards
Vijay


 
Ricky FairRicky Fair
Hi Vijay,
 
Thank you again for your response. 
 
Here is what would be ideal for my organization, so that we can better integrate with SalesForce. I believe this would also be very beneficial for the broader SalesForce userbase/audience:
  1. API service that will provide summary details for documents/attachments/files including ID; Requestor should be able to provide one or more of the below attributes in the request, and a list of associated Documents and their summary should be provided in the response:
    1. Customer ID;
    2. or CustomerName;
    3. or LeadID;
    4. or LeadName;
    5. or UserName;
    6. or UserID;
    7. or UserEmail;
    8. or CaseName;
    9. or CaseID;
    10. or DocumentName;
    11. or DocumentID;
    12. or AttachmentName;
    13. or AttachmentID;
    14. or FileName;
    15. or FileID;
  2. API service that will allow requestor the ability to download one or all documents/attachments/files associated with one or more of the below attributes:
    1. Customer ID;
    2. or CustomerName;
    3. or LeadID;
    4. or LeadName;
    5. or CustomerName;
    6. or LeadID;
    7. or LeadName;
    8. or UserName;
    9. or UserID;
    10. or UserEmail;
    11. or CaseName;
    12. or CaseID;
  3. API service that will allow requestor the ability to delete/remove one or all documents/attachments/files associated with one or more of the below attributes:
    1. Customer ID;
    2. or CustomerName;
    3. or LeadID;
    4. or LeadName;
    5. or CustomerName;
    6. or LeadID;
    7. or LeadName;
    8. or UserName;
    9. or UserID;
    10. or UserEmail;
    11. or CaseName;
    12. or CaseID;
    13. or DocumentName;
    14. or DocumentID;
    15. or AttachmentName;
    16. or AttachmentID;
    17. or FileName;
    18. or FileID;

Thank you,
Ricky
vijay kumar kvijay kumar k
Hi Ricky

If the requestor should give any attribute value you want to find attachment details that are related to attributes?
For example: If you gave CaseId while calling you expecting all the attachments under that caseid?
If yes you must go with custom API it's not possible using SF standard API.
If you fine with custom API all requests are possible which mentioned above.

For Reference:
Example 1: Using ParentId
@RestResource (urlMapping='/GetAttachment*')
global with sharing class GetAttachmentRestAPI {
 
    @HttpGet
    global static Account getattachment() {
        RestRequest req = RestContext.request;
        String parentId = req.params.get('parentid');
        Attachment att = [SELECT Id, Name,body FROM Attachment WHERE ParentId =: parenntid];
        return att;
    }
}

Endpoit URL:htttp://<Domain+salesforce.com>/services/apexrest/GetAttachment?parentid='001324345654532';
  • If you want to delete attachments also you can delete using Delete keyword;
  • If you want to get attachment using attachment you just change the query that's it.
  • If you want to download also you can follow the my earlier comment.
  • The above mentied query not only based on get attachments using parentid, you can use the same based on our needs.