You need to sign in to do that
Don't have an account?
RyanAtTGH
How to create map of Attachment, EmailMessage, and Case to find duplicates
A client is over their storage limit due to large media attachments on emails which are related to cases. When exchanging emails, often the attachments are duplicated. I'd like to identify the duplicate email attachments, based on the parent Email Message assosciation with Case, size, and name.
I can query the attachments, size, and parentId (EmailMessage) using the soql
But, I would also like the Email Message's RelatedTo and/or ParentId field(s) as well, which would give me the Case Id. If an attachment has the same CaseId, BodyLength, and Name, it's 99% a duplicate and I can delete it.
I've tried all the SOQL I know to get it in one query, so I am thinking now I need a custom map or sObject using Apex.
thoughts?
I can query the attachments, size, and parentId (EmailMessage) using the soql
SELECT Id, Name, ContentType, BodyLength, ParentId, Parent.Type FROM Attachment WHERE ContentType = 'image/jpeg' AND ParentId IN (SELECT Id FROM EmailMessage WHERE HasAttachment = TRUE)
But, I would also like the Email Message's RelatedTo and/or ParentId field(s) as well, which would give me the Case Id. If an attachment has the same CaseId, BodyLength, and Name, it's 99% a duplicate and I can delete it.
I've tried all the SOQL I know to get it in one query, so I am thinking now I need a custom map or sObject using Apex.
thoughts?
The query which you have written will be the result of case related attachement. emailMessage ParentId supports for only case.
Thanks!!
Try with below code.
If this helps, Please mark it as best answer.
Thanks!!
So, this returns data about the attachments. I feel I was also successful with this code:
My challenge now, is to have the Case Id, Attachment Name, and Attachment BodyLength in the same row/record, ordered by Case ID and Size, so I can loop over them, and compare if the current one is 'exactly' the same as the one before it, identifying a likely duplicate...