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
LeonardLeonard 

Attachments on Custom Objects

How can I get / set an attachment to a Custom Object? When I inspect my Custom Object, I don't see an Attachment property. I've tried multiple queries with no success.

bob_buzzardbob_buzzard

Attachments contain references to the object that they are attached to, rather than the object have a reference top its attachment.

 

 The attachment object has a parentid field, which is the id of the object it is attached to.

LeonardLeonard
So how do you query for an attachment when pulling down the parent object? Attachment__r gives me an error.
bob_buzzardbob_buzzard

That's because the relationship is from attachment to the object, rather than the other way around, as objects can have any number of attachments.  Thus you have to query to retrieve all attachments like so: 

 

[SELECT Id, Name FROM Attachment WHERE ParentId=:mycustomobject.Id]

 

where mycustomobject is the instance of your custom object.

GreatGombooGreatGomboo

Bob,

 

Thanks for the tips. Can you assist a little further?

 

I want to record the Attachment ID's discovered in a custom object so that I can easily perform some later post processing. How do I get the custom object created with one of the fields defined as the Attachment ID?

 

When I try to create the field the Attachment object is not listed as one of the choices.

 

I appreciate any advice you might offer.