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
Ella Christina Lopez 1Ella Christina Lopez 1 

I am having trouble retrieving data from the email header of the Inbound Email Object. Here's what I've written so far:

Ella Christina Lopez 1Ella Christina Lopez 1
global class (sObject) implements Messaging.InboundEmailHandler {
      global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
          Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
         
          Attendance__c Attendance = new Attendance__c ();
          Attendance.Success_Agent_Name__c = email.fromName.substring(0,email.fromName.indexOf(' '));
      
        //Retrieve date from email header (copy pasted from a post (test code)

         Messaging.InboundEmail.Header header= new Messaging.InboundEmail.Header();
         email.headers=new Messaging.InboundEmail.Header[1];
         header.name='Date'; header.value='';
         email.headers[0]=header;

 
Ella Christina Lopez 1Ella Christina Lopez 1
Now I'm getting an error "Variable does not exist" on this line:
Attendance.Login__c = email.header.date;

Really having trouble retrieving value from the email header guys. Please help!
DebadyutiDebadyuti
Hi Ella,

  I saw you created a new instance of header and that will return null data .Hope below code will help you
global class InboundEmailController implements Messaging.InboundEmailHandler {
      global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
          Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
      
          system.debug('***header*'+email.headers);
        
         //Iterate over email header data
          for(Messaging.InboundEmail.Header header : email.headers){
              //add your logic here 
              //If you debug you will see different header name for each record in list and you need to put a check based on that
          }
        
          
          return result;
  
      }   
          
 }



  Hope it helps.Happy coding :-)
Ella Christina Lopez 1Ella Christina Lopez 1
Thanks, Dibadyuti!

Receiving no error on the code below but it's not working at all. What do you think am missing?

global class EmailtoAttendance implements Messaging.InboundEmailHandler {
      global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
          Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
         
          Attendance__c Attendance = new Attendance__c ();
          Attendance.Success_Agent_Name__c = email.fromName;
          insert Attendance;
          
       
       System.debug('Email Headers : ' + email.Headers);
           for (Messaging.InboundEmail.Header header : email.headers) {
                if (header.name == 'Date') {
                System.debug('Email Headers : ' + header.value);
      }  
}
 
                    
          return result;
      }
  }
DebadyutiDebadyuti
Hi Ella,

  Your code is perfectly fine .It's creating  attendance record populating success_Agent_Name field for me.and in debug log also i got this:
    Email Headers : Fri, 11 Mar 2016 21:45:08 +0530 .

   If your email service is not working please check the configuration that you whitelisted your email service providor like :gmail.com,outlook.com etc.

 Q)How to check that?

-->1)Please go to Email Services and click on your Active Email Service Name.
     2)You should create a email address by clicking on 'New Email Address' .Email address will be autometically generated by salesforce once you save it.It should look somethig like this: 
sfdcemailservice@l-2is5t4qgrw6837l2me8wkjxt2zkxy307hx6lft6ny8vw31fos6.28-1fkbceag.ap2.apex.salesforce.com

*Make sure you fill 'Accept Email From' correctly.For example you are expecting mail from gmail.com then it should populate that as gmail.com.If you keep it black then it may not work properly.

Hope it helps :-)
Ella Christina Lopez 1Ella Christina Lopez 1
It's a bit sad that it's testing well on your end but not on mine. The 'Accept Email From' has now been filled but a new problem has sprung. The mail is now undeliverable and is returning this error message in the email body:

(Undelivered): 554 The apex class EmailtoAttendance failed due to: System.StringException: Invalid id: ...

Please help. :(