I want to query file or files from the inbound and outbound emails in the case: http://take.ms/BxmUw What sobject should I use to query?


Based on your question I think that you want to do something like this:

SELECT Id, CaseNumber,Status,Origin, 
  (SELECT Id, Name FROM Attachments) 
      FROM Case WHERE Origin= Email

This is an example on how to query Cases that have some type of attachment and the origin in Web:

List<Case> caseList =[SELECT Id, CaseNumber,Status,Origin,
                        (SELECT Id, Name FROM Attachments) 
                           FROM Case WHERE Origin = 'Web'];

So you can do the same but the Origin field will be "Email" in your case. If you take a look to the Case object you can see that there is an Origin field. Check the Case Object and take a look to the Origin field.

This type of query will give you what you are looking for...


It will be more difficult if you are trying to get Files. If you are looking for Files you probably need to access the ContentDocument object. That object has a ParentId field and you can probably use it to get to those Files related to Cases. After that you could use a query as the one above to access only those files that came from a "Email- to - case".

On a Case record you can see that there are a few related list. If you have the attachment and the file in your page layout for the Case object you can try this.

Case Realted List

As you can see there is no files either on the Attachment or the Files related lists.

Running this in your developer console:

List<Case> caseList =[SELECT Id, CaseNumber,Status,Origin,
                        (SELECT Id, Name FROM Attachments) 
                           FROM Case WHERE Origin = 'Email'];
system.debug('This is the amount of Case origin Email: ' +caseList);

List<ContentDocument> contentList =[SELECT Id, parentid, Title 
                                    FROM ContentDocument];
system.debug('This is the ammount of ContentDocuments: ' + contentList.size());
for(ContentDocument cd: contentList)
    system.debug('This is the parentId: ' +cd.parentID); 
    system.debug('This is the Titel: ' +cd.Title); 


Will give you these results:

Results One

You can see files that are stored in the ContentDocument object and on the Case object of type "Email"... this is zero, which it is fine.

If you upload a file to the Files related list you will see that it adds that file to the attachment object:

File Upload

If you run the code again in your developer console you will get something funny.

Results Two

You can see the number of Files has increased but the ParentId for that file is "null".... this is probably because, don't know yet :).

The initial conclusion to this is that you probably can start looking for those files related to Cases on the Attachment object because they are probably there anyway. You need to test a bit to finally access those files. As I see it, it is better to get them from the Case object because there is a direct relation to the "Email- to -Case" functionality.

You could also try to query your files using something like this:

List<Case> newListCase =[SELECT Id, CaseNumber,Status,Origin,
                             (SELECT Id, ContentDocument.Title, ContentDocument.Id
                                 FROM AttachedContentDocuments) FROM Case];

The results will be very similar.

  • it is not an attachment object. It's new files functionality that includes sobject like contentdocument... – Eugene Vabishchevich Dec 3 '18 at 9:07
  • Which Object is he final destination of these files? They will be on the Case object? ContentDocuemnt Object? They are coming from Email-to-Case, right? – Carlos Naranjo Dec 3 '18 at 9:21
  • if you send an email to email-to-case with attachment the system creates a new feeditem with file. Email attachment (file in salesforce) – that is what I can't query. – Eugene Vabishchevich Dec 3 '18 at 9:43
  • Are you talking about feedItem in Chatter? – Carlos Naranjo Dec 3 '18 at 10:21

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.