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
kfennkfenn 

Email Services System.NullPointerException Error When Email Received From Online Form

I am a very novice developer and am working on an Email Services class that will parse contact information from applications submitted via our Web site's simple application form. The data is stored in a custom object that we use to track applicants. The email that we receive from the Web site form looks like this:

 

First Name: Joe
Last Name: Test
Position Applying For: Editor/Proofer
EMail Address: test@testemail.com
Address1: 
Address2: 
City: 
State: 
Zip: 
Telephone Number: xxx-xxx-xxxx
Alternate Telephone Number: 
How did you hear about us? Monster.com
If other, please specify: 
Most Recent Employer: Self-Employed

The Email Services code processes this fine when I send this email as a test from my Outlook email client, but when I set up the Web site form to automatically send these applications to the Email Services email address, I receive an email with the following error:

 

"The attached message was sent to the Email Service address <submit_resume@4r0eqtpjrqi4qsx8nryitfmzj.8dnvxeae.c.apex.salesforce.com> but could not be processed because the following error occurred:

 

554 System.StringException: Ending position out of bounds: -1

 

Class.employeeApplication.handleInboundEmail: line 11, column 28 External entry point"

 

What would be different about sending the email from my email address versus having our Web site's form handler send it? My email services code is below for reference:

global class employeeApplication implements Messaging.InboundEmailHandler {
      global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
          Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
          
          SFDC_Employee__c candidate = new SFDC_Employee__c();
          candidate.Employee_Status__c = 'Interested';
          candidate.Initial_Contact_Date__c = System.now().date();
          
          String app = email.plainTextBody;
         
          candidate.Name = app.substring(app.indexof('First Name: ') + 12, app.indexof('Last Name:') - 1) + app.substring(app.indexof('Last Name: ') + 11, app.indexof('Position'));
          candidate.Position__c = app.substring(app.indexof('Position Applying For: ') + 23, app.indexof('EMail Address:'));
          candidate.Email_Address__c = app.substring(app.indexof('EMail Address:') + 15, app.indexof('Address1:'));
          candidate.Home_Address_del__c = app.substring(app.indexof('Address1:') + 10, app.indexof('Address2:')) + app.substring(app.indexof('Address2:') + 10, app.indexof('City:'));
          candidate.Home_City_del__c = app.substring(app.indexof('City:') + 6, app.indexof('State:'));
          candidate.Home_State_del__c = app.substring(app.indexof('State:') + 7, app.indexof('Zip:'));
          candidate.Home_Zip_del__c = app.substring(app.indexof('Zip:') + 5, app.indexof('Telephone Number:'));
          candidate.Home_phone1__c = app.substring(app.indexof('Telephone Number:') + 18, app.indexof('Alternate Telephone Number:'));
          candidate.Personal_Cell_Phone__c = app.substring(app.indexof('Alternate Telephone Number:') + 28, app.indexof('How did you'));
          if (app.substring(app.indexof('How did you hear about us?') + 27, app.indexof('If other,') - 2) == 'Other' ) {
              candidate.Source_of_Candidate__c = app.substring(app.indexof('How did you hear about us?') + 27, app.indexof('If other,') - 2) + ': ' + 
              app.substring(app.indexof('If other, please specify:') + 26, app.indexof('Most Recent Employer:'));
          }
          else {
             candidate.Source_of_Candidate__c = app.substring(app.indexof('How did you hear about us?') + 27, app.indexof('If other,') - 2);
          }
          candidate.Former_Employer_1__c = app.substring(app.indexof('Most Recent Employer:') +22, app.Length());
          
         if (email.fromname == null){
             email.fromname = app.substring(app.indexof('First Name: ') + 12, app.indexof('Last Name:')) + ' ' + app.substring(app.indexof('Last Name: ') + 11, app.indexof('Position'));
         }
         
         if (envelope.fromAddress == null) {
             envelope.fromAddress = app.substring(app.indexof('EMail Address:') + 15, app.indexof('Address1:'));
         }
         
          insert candidate;
          
            if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
              for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
                Attachment attachment = new Attachment();
                // attach to the newly created Employee record
                attachment.ParentId = candidate.Id;
                attachment.Name = email.binaryAttachments[i].filename;
                attachment.Body = email.binaryAttachments[i].body;
                insert attachment;
              }
            }          
          
          
          return result;
      }
  }

 

Best Answer chosen by Admin (Salesforce Developers) 
Andy BoettcherAndy Boettcher

Are you sure your website is sending that email as plain text and not something else?  The error you're getting leads me to think that your string is null...

 

String app = email.plainTextBody;

 

-Andy

All Answers

Andy BoettcherAndy Boettcher

Are you sure your website is sending that email as plain text and not something else?  The error you're getting leads me to think that your string is null...

 

String app = email.plainTextBody;

 

-Andy

This was selected as the best answer
kfennkfenn

That makes sense. Good catch! The email that was sent from the form was definitely an HTML email. However, I switched it to a plain text email and am still getting the same error (and it still works when sent from my email instead of through the form). Are there any other elements that you can think of that could be null as well?

 

Andy BoettcherAndy Boettcher

Is it the same error message?  (It could have changed when you flipped the body to plaintext)

kfennkfenn

Yep, it's the exact same null pointer exception error message. I do get a different error when I send the plain text email from my email address, but that's just because the plain text email included some different line breaks and caused an out of bounds error. If I can figure out the null reference issue when it's sent from the form, I'll just rewrite the class to fix the way it parses the info.

Andy BoettcherAndy Boettcher

Thought:  If you do a replace on line breaks in that string before you pass it through to the "substring" parsers, that could help out too.

 

Also - you could wrap each of your variables in try/catch statements (although that would be a bear for Unit Testing) to try and get all of the info you can instead of just hitting the error.

 

-Andy