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;
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;
}
}
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();
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.
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: ...
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;
Attendance.Login__c = email.header.date;
Really having trouble retrieving value from the email header guys. Please help!
I saw you created a new instance of header and that will return null data .Hope below code will help you
Hope it helps.Happy coding :-)
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;
}
}
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 :-)
(Undelivered): 554 The apex class EmailtoAttendance failed due to: System.StringException: Invalid id: ...
Please help. :(