0

I have created email service and then sending an email to salesforce In debug its showing "Attempt to de-reference a null object error"

contactfrmemailservice@2q1e6c4se7vdyftmuxhwwse18y543iui3kzmpocqg7jl03idve.9e-crmuuac.cs88.apex.sandbox.salesforce.com

global class ContactFrmEmail implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {

        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();


        String con_Email = envelope.fromAddress;
        Contact con = [SELECT Id, Email FROM contact WHERE email =: con_EMail LIMIT 1];
        System.debug('--@@--con--'+con);
       System.debug('--@@--con_Email--'+con_Email);

        }

    result.success = true;
        return result;
    }
 }
2
  • Is your email service active?
    – Reshma
    Feb 10, 2017 at 10:20
  • yes email service active Feb 10, 2017 at 10:22

4 Answers 4

1

Try this it will solve your problem

global class contact_Task_Create_FromEmailService implements Messaging.InboundEmailHandler {
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
      Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

      String emailFrom = envelope.fromAddress;
      List<Contact> conList = [SELECT Id,AccountID,Email FROM Contact WHERE email =: emailFrom LIMIT 1];
      if(conList.size()>0 && conList != null){
          if(conList[0].email == emailFrom.trim()){
              Task t = new Task();
              t.Priority = 'Normal';
              t.status = 'Completed';
              t.whoId = conList[0].id;
              t.whatId = conList[0].AccountID;
              t.subject = email.subject;
              t.activityDate = System.today();
              t.type = 'Email';
              insert t;

          }

      }
      return result;
  }
}
1

On email service, select Accept Email From All email addresses (subject to security settings)

2
  • i have select this still not getting mail and Mail Delivery System <[email protected]> throw email that you message will not send Feb 10, 2017 at 10:22
  • Can you go to Setup | Email Administration | Email to Salesforce Verify that its marked Active Feb 10, 2017 at 10:38
0

The error tells that you have Null Pointer Exception in your Handler code. I would check debug logs for error line number. Btw, the code provided in the question does not compile, is it the current one?

2
  • This could be the culprit- envelope.fromAddress
    – Raul
    Feb 10, 2017 at 12:27
  • Indeed envelope appears to be the only object referenced in the OP that even could be null.
    – Adrian Larson
    Feb 10, 2017 at 12:51
0

finally solved this issue.

global class ContactFrmEmail implements Messaging.InboundEmailHandler {

global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
 Messaging.InboundEnvelope envelope) {

    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

    String con_Email = envelope.fromAddress;


    List<Contact> con = [SELECT Id, Email FROM contact WHERE email =: con_EMail LIMIT 1];


    if(con[0].email == con_Email.trim()) {
        Task T = new Task();
        T.Subject = email.subject;
        T.status = 'closed';
        T.whoId = con[0].id; 
        insert T;
    }

    return result;
}

}

1
  • A code dump adds very little value to the community. Please explain what you actually changed and why it fixed the issue.
    – Adrian Larson
    Feb 10, 2017 at 13:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .