You need to sign in to do that
Don't have an account?
Thomasm
Email Service Attachments
I have setup an email service and have the follow class that when an email is sent it create a record in the custom object ticket but i want it to pull any attachments and save them in the attachment section for that record. I do not geting errors but no attachments are being saved
global class EmailToTicket implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
Ticket__C sTicket = new Ticket__C();
{
sTicket.Job__C = 'a013000000UHggN';
sTicket.Subject__C = email.subject;
sTicket.description__C= email.plainTextBody;
sTicket.Web_Email__C=email.FromAddress;
insert sticket;
if (email.textAttachments != null)
{
for(Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments)
{
Attachment attachment = new Attachment();
attachment.Name = tAttachment.fileName;
attachment.Body = Blob.valueOf(tAttachment.body);
attachment.ParentId = sTicket.Id;
insert attachment;
}
}
//Save any Binary Attachment
if (email.binaryAttachments != null)
{
for(Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
Attachment attachment = new Attachment();
attachment.Name = bAttachment.fileName;
attachment.Body = bAttachment.body;
attachment.ParentId = sTicket.Id;
insert attachment;
}
}
}
result.success = true;
return result;
}
}
global class EmailToTicket implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
Ticket__C sTicket = new Ticket__C();
{
sTicket.Job__C = 'a013000000UHggN';
sTicket.Subject__C = email.subject;
sTicket.description__C= email.plainTextBody;
sTicket.Web_Email__C=email.FromAddress;
insert sticket;
if (email.textAttachments != null)
{
for(Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments)
{
Attachment attachment = new Attachment();
attachment.Name = tAttachment.fileName;
attachment.Body = Blob.valueOf(tAttachment.body);
attachment.ParentId = sTicket.Id;
insert attachment;
}
}
//Save any Binary Attachment
if (email.binaryAttachments != null)
{
for(Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
Attachment attachment = new Attachment();
attachment.Name = bAttachment.fileName;
attachment.Body = bAttachment.body;
attachment.ParentId = sTicket.Id;
insert attachment;
}
}
}
result.success = true;
return result;
}
}
Here is the code you can see that I use for this simple service:
global class ClaimEvidenceEmailServiceHandler implements Messaging.InboundEmailHandler
{
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.Inboundenvelope envelope)
{
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
try
{
Claim_Evidence__c evidence = new Claim_Evidence__c();
evidence.Email_Subject__c = email.subject;
evidence.Email_Text_Body__c = email.plainTextBody;
evidence.Email_From_Address__c = email.FromAddress;
insert evidence;
// Save attachments, if any
List<Attachment> attachments = new List<Attachment>();
if(email.textAttachments != null)
{
for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
Attachment attachment = new Attachment();
attachment.Name = tAttachment.fileName;
attachment.Body = Blob.valueOf(tAttachment.body);
attachment.ParentId = evidence.Id;
attachments.add(attachment);
}
}
if(email.binaryAttachments != null)
{
for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
Attachment attachment = new Attachment();
attachment.Name = bAttachment.fileName;
attachment.Body = bAttachment.body;
attachment.ParentId = evidence.Id;
attachments.add(attachment);
}
}
if(attachments.size() > 0)
{
insert attachments;
}
result.success = true;
}catch(Exception e)
{
result.success = false;
result.message = e.getMessage() + e.getStackTraceString();
}
return result;
}
}