I've got a custom object that might have one or more attachments. I'm looking for a way to report on how many attachments there are on each record.

I've done some research, and it looks like there's no way to do this directly. This blog post from Arkus and this Salesforce success idea, and this paid product in the AppExchange support my impression that it can't be done in Salesforce without extra work of some kind.

Could this be done via a Visualforce page and API calls?


Since you can query for attachments you could create your own VF page that gives you some 'report' like features on the attachment object. Your controller could query for attachments that meet your criteria

list<Attachment> attachs = [Select Id, Name, OwnerId, ParentId, ContentType From Attachment];

The main issue is if you want to use the attachment body. There are a few issues with this. you can't query for the body of an attachment if you are retrieving multiple attachments. From the documentation

You can access all of the fields using a query() call. However, you can't receive the Body field for multiple records in a single query() call. If your query returns the Body field, your client application must ensure that only one row with one Attachment is returned; otherwise, an error occurs. A more effective approach is to return IDs (but not Attachment records in the Body field) from a query() call and then pass them into retrieve() calls that return the Body field.

The other issue is even if you use the retrieve() call to get the body it is encoded in base64, so you have to go through quite a bit of effort to decode the body data. it's doable, but not pretty.

So basically,m if you just want to report on basic metrics around attachments, like number, ContentType, Owners, and Size, then you could do this with a VF page by querying the Attachments records in your controller.

If you want to use the Body of the attachment, you have to get into decoding the Base64. Not impossible by any means, just a bit more complicated than a simple SOQL query with some VF.

  • I hadn't thought about queries as a way to get at attachments. Counting them will probably be enough for my purposes. – Jagular Jun 24 '14 at 21:42

For just a sum of records, you could always create a custom rollup (here meaning, a trigger to populate data). Here's an example:

trigger RollupAttachmentOnX on X (after insert, after update, after delete, after undelete) {
    Map<Id, X> rollup = new Map<Id, X>();
    if(Trigger.new != null) {
        for(Attachment record: Trigger.new) {
            if(record.ParentId.getSObjectType() == X.SObjectType) {
                rollup.put(record.ParentId, null);
    if(Trigger.old != null) {
        for(Attachment record: Trigger.old) {
            if(record.ParentId.getSObjectType() == X.SObjectType) {
                rollup.put(record.ParentId, new X(Attachment_Count__c=0));
    for(AggregateResult record:[SELECT Count(Id) sum, ParentId FROM Attachment
                                WHERE ParentId IN :rollup.keySet()
                                GROUP BY ParentId]) {
        rollup.put((Id)record.get('Id'), new X(Attachment_Count__c=(Double)record.get(sum));
    update rollup.values();

This code assumes that no validation rules will prevent saving the record (you can account for this separately if you need to).

You'll need to do a update on all attachment records once to populate the field value for Attachment Count, after which the platform will manage the number of records on your custom object (named X here).

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.