REST API: Get body of deleted attachment

attachmentrest-api

I have an app that calls the Salesforce REST API to retrieve attachments stored in Salesforce. It makes a call like this to get the attachment body:

/services/data/v52.0/sobjects/Attachment/00Pe0000008ELB4EAO/Body

I now need to do the same for an attachment that may or may not be deleted. I can query a deleted attachment like this and get some information about it:

/services/data/v52.0/queryAll/?q=select+body+from+Attachment+where+id='00Pe0000008ELB4EAO'

This returns a body like this — note this response does not contain the actual file body:

{
    "totalSize": 1,
    "done": true,
    "records": [
        {
            "attributes": {
                "type": "Attachment",
                "url": "/services/data/v52.0/sobjects/Attachment/00Pe0000008ELB4EAO"
            },
            "Body": "/services/data/v52.0/sobjects/Attachment/00Pe0000008ELB4EAO/Body"
        }
    ]
}

But when I call /services/data/v52.0/sobjects/Attachment/00Pe0000008ELB4EAO/Body, I get a 404 Not Found status and the following response body:

[
    {
        "errorCode": "NOT_FOUND",
        "message": "The requested resource does not exist"
    }
]

Are there any workarounds I can consider?

Best Answer

It looks like when an attachment is deleted (Attachment.IsDeleted = true (services/data/v52.0/queryAll/?q=select+IsDeleted+,+body+from+Attachment+where+id='00Pe0000008ELB4EAO') ), the record goes to the Recycle Bin and the body gets hidden.

One of the option would be to restore the attachment, get the body, and delete the attachment again? Although it's not perfect from the audit point of view (the LastModifiedById and LastModifiedDate fields will be changed)

Related Topic