I need to export all of the attachments of an org. I would like to break them down to be in a folder based upon the object type of their parent. In other words, I want all attachments which are in an Account to be in one single folder, and all of the ones in an Opportunity to be in another folder.

I've been trying to determine how the Attachments are arranged by running queries on the Attachment object in Workbench. But I believe the closest I can come to determining this information is the Attachment object's ParentID field. But that's just the ID of the individual record, not the object type of the Parent. Is there any way to determine the object type of the Parent of the Attachment ?


In a query, you can pull Parent.Type in your SELECT clause, or even filter on it in your WHERE clause.

SELECT Body, Parent.Type FROM Attachment

Quite easy, just call getsobjecttype() on that Id, and it will return API name of that object.

Id parentId= '00161000000Mjya'; 
System.debug('object is '+ parentId.getsobjecttype()); //Returns Account

Src: https://help.salesforce.com/articleView?id=000340200&language=en_US&type=1&mode=1

  • Thank you very much Pranay. I'm not quite sure how to incorporate that into my query, or my actual export. I'm going to use dataloader.io for the export, and it doesn't really have a way to filter based on that - or at least not from what I see. Any thoughts ? – Zoom_v Jul 18 at 15:24

Just to add on the answer about getsobjecttype() :

You can also check this video which explains how to export attachments from salesforce using NodeJS.

You can adapt the code by adding query filter on object type and organise the file export in appropriate folder.

Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.