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
Devendra_07Devendra_07 

query attachments from case object

Hello Everyone,

I have a situation where i'm using the email-to-case. The scenerio is : 
1. If i use email-to -case then i'm not able to fetch the attachments from Case object
query --> SELECT CaseNumber, (Select Name from Attachments) from Case
This doesn't return any attachment.
2. If i use custom apex class :- inboundemailhandler class, I'm receiving the attachments from email and also able to fetch the attachments using the same query mentioned above..

Code --> global class GetEmailToSalesForce implements Messaging.InboundEmailHandler {   
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.Inboundenvelope envelope) {    
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        Case newCase = new Case();
        newCase.SuppliedName = email.plainTextBody;
        newCase.SuppliedEmail = envelope.fromAddress;
        newCase.Subject = email.subject;
        newCase.Status = 'New';
        newCase.Origin = 'Email';
        try{
        insert newCase;
            System.debug('New Case is  created:'+newCase.Id);
        }catch(Exception e){
            system.debug('Got an exception:'+e);
        }
        if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
            for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
                Attachment attachment = new Attachment();
                //attachment.ParentId = contact.Id;
                attachment.ParentId = newCase.Id;
                attachment.Name = email.binaryAttachments[i].filename;
                attachment.Body = email.binaryAttachments[i].body;
                insert attachment;
            }
        }
        
        return result;
    }
}

Can someone please let me know the reason.

Thanks.
Best Answer chosen by Devendra_07
{tushar-sharma}{tushar-sharma}
Becuase in email-to-case, Attachment are child of EmailMEssage records and not the Case record. In your code you have created InboundEmailHandler and manually linkeing the Attachment with case. Thats why you get the result.

First query childEmailMessage record and then query Attachment, you will get the result.

If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/

All Answers

{tushar-sharma}{tushar-sharma}
Becuase in email-to-case, Attachment are child of EmailMEssage records and not the Case record. In your code you have created InboundEmailHandler and manually linkeing the Attachment with case. Thats why you get the result.

First query childEmailMessage record and then query Attachment, you will get the result.

If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/
This was selected as the best answer
Devendra_07Devendra_07
Hey tushar,

I have tried to query but i'm not able to fetch the attachments : 

Query --> SELECT ID, Name FROM Attachment WHERE ParentId IN (
    SELECT Id FROM EmailMessage WHERE ParentId = '5002w0000XXXXXXXXX'
)

P.S: Here ParentId is Case record ID

Can you please elaborate more now...

Thanks.