0

I am getting below error while sending an email to the mail address generated in the email service

test@k-2jdla25gm4keaulec1vyjtpuyozqqprsn0mnie0kqwew3z3fmz.28-1glq6eac.ap2.apex.salesforce.com (Undelivered): 554 The apex class CreateContactFrmEmail failed due to: System.NullPointerException: Attempt to de-reference a null object.

class

/**
 * Email services are automated processes that use Apex classes
 * to process the contents, headers, and attachments of inbound
 * email.
 */
global class CreateContactFrmEmail 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;
    }
}

Not sure what the issue is.

6
  • Which line...you can make your question a lot easier to answer if you identify the line that throws this error.
    – Adrian Larson
    Feb 20, 2017 at 12:12
  • It doesn't look like the class failed as I could not see any errors in the debug logs.I think there is a problem with the email address.Not sure what it is.The message didn't get delivered only. "A message that you sent could not be delivered to one or more of its recipients. This is a permanent error. The following address(es) failed:" Feb 20, 2017 at 12:15
  • 1
    Try to send an email with a binary attachment. If that works, then your issue is with checking if there are attachments before you go into you for loop. (That fixed it for me once.)
    – Kasper
    Feb 20, 2017 at 12:20
  • @Kasper: You are correct.The issue was there was no null checking for the attachment and the use case for which I was testing didn't contain any attachments.I added the null check and it worked like a charm.....Thanks a lot.Keep Rocking! Feb 20, 2017 at 12:35
  • Happy to help. A few weeks ago i wrote an email handler class myself so this was an easy one for me. Let me write up an official answer and than we can mark this as resolved.
    – Kasper
    Feb 20, 2017 at 12:37

1 Answer 1

1

Normally you can use a for loop on a empty list and nothing bad happens. Unfortunately this isn't the case for either binary attachments or plain text attachments in an email handler class. To use those in a for loop you first need to check that there are attachments. If you add this before you for loop it should be fixed:

if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {

If you'd like some more information on email handler classes check this introduction out: http://blog.jeffdouglas.com/2010/03/12/writing-an-inbound-email-service-for-salesforce-com/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.