You need to sign in to do that
Don't have an account?

Struggling With Attachments in Messaging.InboundEmailHandler{}
Hi All,
I'm getting a null exception error on line 27 when I look for email.binaryAttachments.size(). I'm attaching files to my emails before I send them, but they're not attaching to the Lead.
Apex
I'm getting a null exception error on line 27 when I look for email.binaryAttachments.size(). I'm attaching files to my emails before I send them, but they're not attaching to the Lead.
Apex
global class EmailDemoReceive implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInBoundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){ Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); if([select count() from Lead where Name = :email.subject]==0){ Lead leadz = new Lead(); String addy = email.subject; if(String.isNotEmpty(addy)){ List<String> nameParts = addy.split('\\bCompany=\\b'); String namePart = addy.remove('Company='); System.debug(namePart); system.debug(nameParts); leadz.LastName = email.fromName; leadz.Company = namePart; String chopit = leadz.company; system.debug(leadz.Company); leadz.Email = email.fromAddress; leadz.Description = email.htmlBody; insert leadz; system.debug(email.binaryAttachments); for(Integer i = 0; i < email.binaryAttachments.size(); i++){ Attachment attach = new attachment(); attach.ParentId = leadz.id; attach.Name = email.binaryAttachments[i].filename; attach.Body = email.binaryAttachments[i].body; System.debug(attach); insert attach; system.debug(attach.Id); } } else{ leadz.LastName = email.fromName; System.debug(leadz.Company); leadz.Company = 'Unknown'; System.debug(leadz.Company); leadz.Email = email.fromAddress; leadz.Description = email.htmlBody; insert leadz; } } return result; } }
All Answers
Please use below code and let me know if it works.
global class EmailDemoReceive implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInBoundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
if([select count() from Lead where Name = :email.subject]==0){
Lead leadz = new Lead();
String addy = email.subject;
if(String.isNotEmpty(addy)){
List<String> nameParts = addy.split('\\bCompany=\\b');
String namePart = addy.remove('Company=');
System.debug(namePart);
system.debug(nameParts);
leadz.LastName = email.fromName;
leadz.Company = namePart;
String chopit = leadz.company;
system.debug(leadz.Company);
leadz.Email = email.fromAddress;
leadz.Description = email.htmlBody;
insert leadz;
system.debug(email.binaryAttachments);
if (email.binaryAttachments != null && email.binaryAttachments.size() > 0)
{
for(Integer i = 0; i < email.binaryAttachments.size(); i++){
Attachment attach = new attachment();
attach.ParentId = leadz.id;
attach.Name = email.binaryAttachments[i].filename;
attach.Body = email.binaryAttachments[i].body;
System.debug(attach);
insert attach;
system.debug(attach.Id);
}
}else if (email.textAttachments != null && email.textAttachments.size() > 0)
{
for (integer i = 0 ; i < email.textAttachments.size() ; i++)
{
Attachment txtAttachment = new Attachment();
txtAttachment.Name = email.textAttachments[i].filename;
txtAttachment.Body = Blob.valueOf(email.textAttachments[i].Body);
txtAttachment.ParentId = emailMessage.id;
insert txtAttachment;
}
}
}
else{
leadz.LastName = email.fromName;
System.debug(leadz.Company);
leadz.Company = 'Unknown';
System.debug(leadz.Company);
leadz.Email = email.fromAddress;
leadz.Description = email.htmlBody;
insert leadz;
}
}
return result;
}
}
Best Regards,
-Vivek
Please check attachment, under email message and let me know if it works.
Best Regards,
-Vivek
Please Try below code and let me know.
global class EmailDemoReceive implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInBoundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
if([select count() from Lead where Name = :email.subject]==0){
Lead leadz = new Lead();
String addy = email.subject;
if(String.isNotEmpty(addy)){
List<String> nameParts = addy.split('\\bCompany=\\b');
String namePart = addy.remove('Company=');
System.debug(namePart);
system.debug(nameParts);
leadz.LastName = email.fromName;
leadz.Company = namePart;
String chopit = leadz.company;
system.debug(leadz.Company);
leadz.Email = email.fromAddress;
leadz.Description = email.htmlBody;
insert leadz;
emailMessage.FromName = email.fromName;
emailMessage.Subject = email.Subject;
if(email.htmlBody != null){
if(email.htmlBody.length()>32000){
emailMessage.htmlBody = email.htmlBody.substring(0,21999);}
else{
emailMessage.HtmlBody = email.htmlBody;
}
}
if(email.plainTextBody != null){
if(email.plainTextBody.length()>32000){
emailMessage.TextBody = email.plainTextBody.substring(0,21999);}
else{
emailMessage.TextBody = email.plainTextBody;}
}
emailMessage.Incoming = true;
emailMessage.ParentId = leadz.id;
emailMessage.FromAddress = email.fromAddress;
system.debug(email.binaryAttachments);
if (email.binaryAttachments != null && email.binaryAttachments.size() > 0)
{
for(Integer i = 0; i < email.binaryAttachments.size(); i++){
Attachment attach = new attachment();
attach.ParentId = leadz.id;
attach.Name = email.binaryAttachments[i].filename;
attach.Body = email.binaryAttachments[i].body;
System.debug(attach);
insert attach;
system.debug(attach.Id);
}
}else if (email.textAttachments != null && email.textAttachments.size() > 0)
{
for (integer i = 0 ; i < email.textAttachments.size() ; i++)
{
Attachment txtAttachment = new Attachment();
txtAttachment.Name = email.textAttachments[i].filename;
txtAttachment.Body = Blob.valueOf(email.textAttachments[i].Body);
txtAttachment.ParentId = emailMessage.id;
insert txtAttachment;
}
}
}
else{
leadz.LastName = email.fromName;
System.debug(leadz.Company);
leadz.Company = 'Unknown';
System.debug(leadz.Company);
leadz.Email = email.fromAddress;
leadz.Description = email.htmlBody;
insert leadz;
}
}
return result;
}
}
Best Regards,
-Vivek
global class EmailDemoReceive implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInBoundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
if([select count() from Lead where Name = :email.subject]==0){
Lead leadz = new Lead();
String addy = email.subject;
if(String.isNotEmpty(addy)){
List<String> nameParts = addy.split('\\bCompany=\\b');
String namePart = addy.remove('Company=');
System.debug(namePart);
system.debug(nameParts);
leadz.LastName = email.fromName;
leadz.Company = namePart;
String chopit = leadz.company;
system.debug(leadz.Company);
leadz.Email = email.fromAddress;
leadz.Description = email.htmlBody;
insert leadz;
emailMessage.FromName = email.fromName;
emailMessage.Subject = email.Subject;
if(email.htmlBody != null){
if(email.htmlBody.length()>32000){
emailMessage.htmlBody = email.htmlBody.substring(0,21999);}
else{
emailMessage.HtmlBody = email.htmlBody;
}
}
if(email.plainTextBody != null){
if(email.plainTextBody.length()>32000){
emailMessage.TextBody = email.plainTextBody.substring(0,21999);}
else{
emailMessage.TextBody = email.plainTextBody;}
}
emailMessage.Incoming = true;
emailMessage.ParentId = leadz.id;
emailMessage.FromAddress = email.fromAddress;
insert emailMessage;
system.debug(email.binaryAttachments);
if (email.binaryAttachments != null && email.binaryAttachments.size() > 0)
{
for(Integer i = 0; i < email.binaryAttachments.size(); i++){
Attachment attach = new attachment();
attach.ParentId = leadz.id;
attach.Name = email.binaryAttachments[i].filename;
attach.Body = email.binaryAttachments[i].body;
System.debug(attach);
insert attach;
system.debug(attach.Id);
}
}else if (email.textAttachments != null && email.textAttachments.size() > 0)
{
for (integer i = 0 ; i < email.textAttachments.size() ; i++)
{
Attachment txtAttachment = new Attachment();
txtAttachment.Name = email.textAttachments[i].filename;
txtAttachment.Body = Blob.valueOf(email.textAttachments[i].Body);
txtAttachment.ParentId = emailMessage.id;
insert txtAttachment;
}
}
}
else{
leadz.LastName = email.fromName;
System.debug(leadz.Company);
leadz.Company = 'Unknown';
System.debug(leadz.Company);
leadz.Email = email.fromAddress;
leadz.Description = email.htmlBody;
insert leadz;
}
}
return result;
}
}
Best Regards,
-Vivek
global class EmailDemoReceive implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInBoundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
if([select count() from Lead where Name = :email.subject]==0){
Lead leadz = new Lead();
String addy = email.subject;
if(String.isNotEmpty(addy)){
List<String> nameParts = addy.split('\\bCompany=\\b');
String namePart = addy.remove('Company=');
System.debug(namePart);
system.debug(nameParts);
leadz.LastName = email.fromName;
leadz.Company = namePart;
String chopit = leadz.company;
system.debug(leadz.Company);
leadz.Email = email.fromAddress;
leadz.Description = email.htmlBody;
insert leadz;
EmailMessage emailMessage = new EmailMessage();
emailMessage.FromName = email.fromName;
emailMessage.Subject = email.Subject;
if(email.htmlBody != null){
if(email.htmlBody.length()>32000){
emailMessage.htmlBody = email.htmlBody.substring(0,21999);}
else{
emailMessage.HtmlBody = email.htmlBody;
}
}
if(email.plainTextBody != null){
if(email.plainTextBody.length()>32000){
emailMessage.TextBody = email.plainTextBody.substring(0,21999);}
else{
emailMessage.TextBody = email.plainTextBody;}
}
emailMessage.Incoming = true;
emailMessage.ParentId = leadz.id;
emailMessage.FromAddress = email.fromAddress;
insert emailMessage;
system.debug(email.binaryAttachments);
if (email.binaryAttachments != null && email.binaryAttachments.size() > 0)
{
for(Integer i = 0; i < email.binaryAttachments.size(); i++){
Attachment attach = new attachment();
attach.ParentId = leadz.id;
attach.Name = email.binaryAttachments[i].filename;
attach.Body = email.binaryAttachments[i].body;
System.debug(attach);
insert attach;
system.debug(attach.Id);
}
}else if (email.textAttachments != null && email.textAttachments.size() > 0)
{
for (integer i = 0 ; i < email.textAttachments.size() ; i++)
{
Attachment txtAttachment = new Attachment();
txtAttachment.Name = email.textAttachments[i].filename;
txtAttachment.Body = Blob.valueOf(email.textAttachments[i].Body);
txtAttachment.ParentId = emailMessage.id;
insert txtAttachment;
}
}
}
else{
leadz.LastName = email.fromName;
System.debug(leadz.Company);
leadz.Company = 'Unknown';
System.debug(leadz.Company);
leadz.Email = email.fromAddress;
leadz.Description = email.htmlBody;
insert leadz;
}
}
return result;
}
}
Best Regards,
-Vivek
Getting an error:
FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Case ID: id value of incorrect type: 00QF000000uQelHMAS: [ParentId]
So is it not possible to attach an email message to a lead?
Howevre you try to send eamil with text attachment then it will attachment to lead but it will not work for binary attachment.
global class EmailDemoReceive implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInBoundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
if([select count() from Lead where Name = :email.subject]==0){
Lead leadz = new Lead();
String addy = email.subject;
if(String.isNotEmpty(addy)){
List<String> nameParts = addy.split('\\bCompany=\\b');
String namePart = addy.remove('Company=');
System.debug(namePart);
system.debug(nameParts);
leadz.LastName = email.fromName;
leadz.Company = namePart;
String chopit = leadz.company;
system.debug(leadz.Company);
leadz.Email = email.fromAddress;
leadz.Description = email.htmlBody;
insert leadz;
EmailMessage emailMessage = new EmailMessage();
emailMessage.FromName = email.fromName;
emailMessage.Subject = email.Subject;
if(email.htmlBody != null){
if(email.htmlBody.length()>32000){
emailMessage.htmlBody = email.htmlBody.substring(0,21999);}
else{
emailMessage.HtmlBody = email.htmlBody;
}
}
if(email.plainTextBody != null){
if(email.plainTextBody.length()>32000){
emailMessage.TextBody = email.plainTextBody.substring(0,21999);}
else{
emailMessage.TextBody = email.plainTextBody;}
}
emailMessage.Incoming = true;
emailMessage.ParentId = leadz.id;
emailMessage.FromAddress = email.fromAddress;
//insert emailMessage;
system.debug(email.binaryAttachments);
if (email.binaryAttachments != null && email.binaryAttachments.size() > 0)
{
for(Integer i = 0; i < email.binaryAttachments.size(); i++){
Attachment attach = new attachment();
attach.ParentId = leadz.id;
attach.Name = email.binaryAttachments[i].filename;
attach.Body = email.binaryAttachments[i].body;
System.debug(attach);
insert attach;
system.debug(attach.Id);
}
}else if (email.textAttachments != null && email.textAttachments.size() > 0)
{
for (integer i = 0 ; i < email.textAttachments.size() ; i++)
{
Attachment txtAttachment = new Attachment();
txtAttachment.Name = email.textAttachments[i].filename;
txtAttachment.Body = Blob.valueOf(email.textAttachments[i].Body);
txtAttachment.ParentId = emailMessage.id;
insert txtAttachment;
}
}
}
else{
leadz.LastName = email.fromName;
System.debug(leadz.Company);
leadz.Company = 'Unknown';
System.debug(leadz.Company);
leadz.Email = email.fromAddress;
leadz.Description = email.htmlBody;
insert leadz;
}
}
return result;
}
}
Best Regards,
-Vivek
16:58:31:135 FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Case ID: id value of incorrect type: 00QF000000uQf2sMAC: [ParentId]