You need to sign in to do that
Don't have an account?
![Devendra_07 Devendra_07](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
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.
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.
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
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/
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.