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
jaanvivekjaanvivek 

Error While using Inbound Email service

Hello All,

 

I am trying to use Email Service, for this  I have created an Email Service in my SFDC instance.

 

I have created one Apex class as below mentioned-

global class inBoundEmail implements Messaging.InboundEmailHandler
{
   global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope)
   {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        String subToCompare = 'Create Contact';

        if(email.subject.equalsIgnoreCase(subToCompare))
        {
            Contact c = new Contact();
            c.LastName = email.plainTextBody;
            insert c;
            // Save attachments, if any
            for(Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
            Attachment attachment = new Attachment();

            attachment.Name = tAttachment.fileName;
            attachment.Body = Blob.valueOf(tAttachment.body);
            attachment.ParentId = c.Id;
            insert attachment;
            }

            //Save any Binary Attachment
            for(Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
            Attachment attachment = new Attachment();

            attachment.Name = bAttachment.fileName;
            attachment.Body = bAttachment.body;
            attachment.ParentId = c.Id;
            insert attachment;
            }
        }

        result.success = true;
        return result;
             
   }
  }

  But while sending email from my email id to Create Contact , I'm getting below mentioned error

 

The attached message was sent to the Email Service address <createcontact@o-kkmoyp3dd260lsz0qd262mitw6wxvwfpozgoqdwqpr58npllg.9-ekpgea2.9.apex.salesforce.com> but could not be processed because the following error occurred:

554 System.NullPointerException: Attempt to de-reference a null object

Class.inBoundEmail.handleInboundEmail: line 14, column 1

  I'm following below steps while sending email..

 

ToAddress-  createcontact@o-kkmoyp3dd260lsz0qd262mitw6wxvwfpozgoqdwqpr58npllg.9-   ekpgea2.9.apex.salesforce.com

 

Subject- Create Contact

 

Email Body- Contact Name.

 

Could any one help me to get this ok. So that i can see  inserted contact in Contacts tab.

 

Thanks & regards,

JaanVivek

Best Answer chosen by Admin (Salesforce Developers) 
HariDineshHariDinesh

 

 

Hi,

Here the problem is the attachment is receiving as Null.

You should check whether there is attachment is there or not in Email.Textattachment before using it.

Please add below highlighted code to your code so that Null reference problem will be solved.

 

 insert c;
            // Save attachments, if any
            system.debug('#####email.textAttachments'+email.textAttachments);
            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 = c.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 = c.Id;
            insert attachment;
            }}

 

Find the CooK Book Recipe also regarding this.

http://developer.force.com/cookbook/recipe/retrieving-email-attachments-and-associating-them-with-records

 

The Main problem here is even you are attaching an attachment it is not considering it as attachment.

I found similar type of issues

Find the below URL for this

http://success.salesforce.com/questionDetail?qId=a1X30000000IICXEA4