Salesforce Get Attachment Body

I'm trying to get the body of an Attachment object in Salesforce so I can provide it to the user via utils.downloadFile(). After a lot of research and trial and error it seems it is possible to access this through a Salesforce CRUD Action with:

CRUD Action = Retrieve
Resource Type = Attachment
Resource ID = /Body

Per this Salesforce documentation, this request will return the binary content of the attachment. However this seems to be getting converted to ASCII data before I can access it. The query.data field is:

result:

It's not base64 encoded, and I can't figure out any way to serve it to the user as a file that's not corrupted.

Is there another way to access binary data from Salesforce? I'm considering connecting the Salesforce REST API directly as described here, but I'd rather not do that if I don't have to.

Hey @cgw - the short answer is that there isn't a very convenient way to do this, as Retool isn't really equipped to handle raw binary responses. I'm not even able to replicate the setup that you've outlined above, probably because I'm testing on a Lightning instance. Any attachments I upload are created as ContentDocument objects.

If you expect your users to be authenticated in Salesforce, though, one option is to just construct and give them a URL to the relevant attachment.

Have you had a chance to revisit this, @cgw?

Thanks for following up.

I came up with an ugly workaround, by making the service generating the document also put a copy in S3, which is easier to access and serve to the user. The specific purpose of this was to pull an attachment out of Salesforce for a user without an account, so pointing them at the URL in Salesforce wouldn't have worked.

1 Like

I'm glad you found a decent workaround! File management in something like S3 is generally way easier to integrate, but I understand the desire to keep everything in one place.

I've gone ahead and marked your post as the "Solution", but will keep the topic open so that I can share any future updates and so other community members can chime in. :+1: