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
sctdevsctdev 

Email services tutorial

I have run through the email services tutorial several times with no successful email update of mileage. I do not find anywhere that an email was received. Is there a step missing from the tutorial? The emails do not bounce back, is there a log somewhere I can look for errors or status?

 

thanks

 

SCT

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

 I just tested the code below with the code I posted yesterday, and it adds an attachment of type PDF to the Contact along with the task when I attach a PDF to the email. Note - you also have to enable the email service to accept text and binary attachments.

 

 

if (email.binaryAttachments!=null && email.binaryAttachments.size() > 0) { for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) { System.debug('Binary Attachments - filename: ' + email.binaryAttachments[i].filename); System.debug('Binary Attachments - size: ' + email.binaryAttachments[i].mimeTypeSubType); Attachment a = new Attachment(ParentId = ConId, Name = email.binaryAttachments[i].filename, Body = email.binaryAttachments[i].body); insert a; } }

 

 

 

All Answers

BritishBoyinDCBritishBoyinDC
Is this for inbound or outbound email services?
sctdevsctdev
Incoming emails with mileage information.
BritishBoyinDCBritishBoyinDC

Two things to try...are you using email address filtering (i.e. only accepting from certain emails?) - if so, you have to remember to add the address/domain to the Email Service AND to the actual special email address it generates - click Edit next to the email address.

 

Alos, what does the test script say? Does it successfully create it? Are you using a try-catch on the insert statement, in case it is the new object that is failing?

 

If you don't have one, start with something simple like this, but you can also access the result object to see what is happening...

 

 

static testMethod void testEmailMileage() { // Create a new email and envelope object Messaging.InboundEmail email = new Messaging.InboundEmail(); Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); // Create the plainTextBody and fromAddres for the test email.plainTextBody = 'Here is my plainText body of the email'; email.fromAddress ='rmencke@salesforce.com'; // call the class and test it with the data in the testMethod CreateSchedulingRequestExample emailServiceObj = new CreateSchedulingRequestExample(); emailServiceObj.handleInboundEmail(email, env ); }

 

 

 

sctdevsctdev

In your sample code to 'call the class and test it with the data in the testMethod', what do I set emailServiceObj if I am using the EmailToApex class example?

 

The code in the example is:

/** * Email services are automated processes that use Apex classes * to process the contents, headers, and attachments of inbound * email. */ global class EmailToApex implements Messaging.InboundEmailHandler { public static String getFieldValue(String plainTextBody, String pLabel) { Integer startPos = plainTextBody.indexOf(pLabel); Integer endPos = plainTextBody.indexOf('\n'); return plainTextBody.substring(startPos+pLabel.length(), endPos); } global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope envelope) { Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); String contactName = email.subject; Double.valueOf(getFieldValue(email.plainTextBody, 'Mileage:')); List<Contact> contactResult = new List<Contact>(); contactName = '%'+contactName+'%'; Mileage__c[] newMileage = new Mileage__c[0]; try { for (Contact c : [Select Id, Name, Email From Contact Where Name like :contactName Limit 1]) { newMileage.add(new Mileage__c(miles__c = mileageInt, Contact__c = c.Id)); } insert newMileage; } catch (System.Exception e) { System.debug('Error: ' + e); } result.success = true; // If false, an email can be sent back with a message. return result; } }

 

thanks,
BritishBoyinDCBritishBoyinDC

Try this:

 

static testMethod void testEmailMileage() {// Create a new email and envelope object Messaging.InboundEmail email = new Messaging.InboundEmail(); Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();// Create the plainTextBody and fromAddres for the test email.plainTextBody = 'Here is my plainText body of the email'; email.fromAddress ='rmencke@salesforce.com'; // call the class and test it with the data in the testMethod EmailToApex emailServiceObj = new EmailToApex(); emailServiceObj.handleInboundEmail(email, env ); }

 

 

 

sctdevsctdev

That compiles fine. But it really does not like this line:

    email.plainTextBody = 'Mileage';

 

Give me System.StringException: Starting position out of bounds:

 

 

It also complained about all commented lines.

 

bubkirchnerbubkirchner

Hello,

 

 

I'm running into the same problem.  Did you ever get your Inbound emails to work for the MileageTracker sample?  Is there a way to troubleshoot this problem?

 

Thanks,

John Kirchner

jkirchner@cort.com

PamSalesforcePamSalesforce

Even i am getting the same error

String from method public static String getFieldValue(String, String) in 0 ms
System.StringException: Ending position out of bounds: -1

 

Anyone who has solveds this problem.Please do reply.

BritishBoyinDCBritishBoyinDC

This might help - This works in my dev environment. It looks up the contact based on from email, and creates a task for the contact. It sets the description of the task to body of the email. I send an email to the email service associated with this class, and it set the text as designed.

 

The test class also works.

 

 

global class CreateTaskEmailExample implements Messaging.InboundEmailHandler {

global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
Messaging.InboundEnvelope env){

// Create an inboundEmailResult object for returning the result of the Apex Email Service
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

String myPlainText= '';

// Add the email plain text into the local variable

myPlainText = email.plainTextBody;

// New Task object to be created

Task[] newTask = new Task[0];

// Try to lookup any contacts based on the email from address
Contact [] vCon = [Select Id, Name, Email From Contact Where Email = :email.fromAddress Limit 1];
Id Conid;

 

If (vCon.size() > 0) {
ConId = vCon[0].Id;
}

// Add a new Task to the contact record we just found above.
newTask.add(new Task(Description = myPlainText,
Priority = 'Normal',
Status = 'Inbound Email',
Subject = email.subject,
IsReminderSet = true,
ReminderDateTime = System.now()+1,
WhoId = ConId));

// Insert the new Task
insert newTask;

//Check creation

Task t = [Select Id, Description from Task where Id = :newTask[0].Id limit 1];
system.debug(t.Description);

// Set the result to true. No need to send an email back to the user
// with an error message

result.success = true;

// Return the result for the Apex Email Service
return result;

}
static testMethod void testEmailMileage() {

// Create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

// Create the plainTextBody and fromAddres for the test
email.plainTextBody = 'Here is my plainText body of the email';
email.fromAddress ='rmencke@salesforce.com';

// call the class and test it with the data in the testMethod
CreateTaskEmailExample emailServiceObj = new CreateTaskEmailExample ();
emailServiceObj.handleInboundEmail(email, env );

}
}

 

 

 

Message Edited by BritishBoyinDC on 09-10-2009 09:25 PM
PamSalesforcePamSalesforce

Hi,

 

I am now able to create the record. The problem was that when i was sending the mail the email body had Miles: 40.

The email body should have Mile: 40 followed by  a new line ('\n'), coz the code marks the end with '\n'.

 

But i have another issue. I am sending a .pdf attachement as well in my email and i want to relate the pdf with the record created. Please help.

 

Thanks.

BritishBoyinDCBritishBoyinDC
Do a search for InboundEmail.BinaryAttachment Object in the docs/message boards - there are some examples of how to retrieve the attachments and associate them to a record...
PamSalesforcePamSalesforce

Hi,

 

Thanks for the update. I tried a couple of them it doesnot work. Also there are no example for pdf attachments.

 Kindly post the code to get pdf attachments from Inbound email.

 

Thanks,

 

BritishBoyinDCBritishBoyinDC

 I just tested the code below with the code I posted yesterday, and it adds an attachment of type PDF to the Contact along with the task when I attach a PDF to the email. Note - you also have to enable the email service to accept text and binary attachments.

 

 

if (email.binaryAttachments!=null && email.binaryAttachments.size() > 0) { for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) { System.debug('Binary Attachments - filename: ' + email.binaryAttachments[i].filename); System.debug('Binary Attachments - size: ' + email.binaryAttachments[i].mimeTypeSubType); Attachment a = new Attachment(ParentId = ConId, Name = email.binaryAttachments[i].filename, Body = email.binaryAttachments[i].body); insert a; } }

 

 

 

This was selected as the best answer
PamSalesforcePamSalesforce

Hi,

 

Thanks for the help. My email service was not enabled to accept attachment. Once i modified the email service to accept attacments, it worked.

 

Thanks,