3

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?

5 Answers 5

4

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.

1
  • 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, 2014 at 21:42
3

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).

0

Sharing my bit of knowledge in 2024:

Salesforce still partially supports reporting on notes and attachments/files. Reference: https://help.salesforce.com/s/articleView?id=000383957&type=1

You can use Salesforce Partner Apps available on AppExchange to overcome this limitation. An App has been developed by our company to overcome this limitation. I recommend you to use that - Lazy Admin App, an AI-Powered Quick Reporting Tool. It generates reports faster than usual and supports relational data as well. Additionally, it offers chart visualization and data analysis features.

Alternatively, you could acheive this using REST API or third party apps like DonkeyApp. Respell.io, and more.

7
  • 1
    You should be more clear that you are affiliated with the company that provides that Lazy Admin App.
    – Derek F
    May 7 at 16:24
  • I'll retract my downvote when you edit to add that disclosure.
    – Derek F
    May 7 at 17:34
  • That is true. However, I didn't comment here as a company's employee. Rather a solution to a problem which this App solves effectively. May 8 at 19:51
  • I didn't comment here as an employee, this is impossible to prove and this situation is a conflict of interest. The fact is that you are employed by that company, and you are biased (if not outright incentivized) to promote your own wares. You need to disclose the fact, in the answer body via an edit, that you are in fact an employee. I will flag this as spam if you fail to do this within 24 hours of this comment's creation.
    – Derek F
    May 9 at 12:27
  • 2
    Assadullah Shaikh, please use your original user to update your post. I've just rejected your clarification, because you used a different user account. May 10 at 14:12
0

You might want to consider the declarative roll-up summary tool. https://sfdo-community.github.io/declarative-lookup-rollup-summaries/

You would have to build a roll-up for each object but it would work.

Really depends on what you are trying to accomplish.

You could build your own VF and API but the dlrs is a great tool that requires no code and once you have that field on the object with the attachment count you can then do other fun things using standard sf.

-5

Hope all of you are doing well!

I have developed an app which can help you in getting the required information. This app works for all Custom as well as Standard object. By using this app you can -

Get information like which Account have Attachments and which doesn't have. Same goes to custom objects as well. Get information like which Account have Files and which doesn't have. Same goes to custom objects as well. Gell all Files infomation in your org Get all Attachment information in your org. Download the report as CSV File. Save the report for later use. Please feel free to use this app as it is completely free to use. Your suggestions are most welcome.

You can download the app from here -

https://sfdcfilemanager.com/download-app/

New contributor
Sumit is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .