I have to do a email service that when I send a mail to my salesforce email service, it creates a contact with the first and lastname of the people who send, and attach the email to the contact created. I have the following code where I can save an attachment file, but I want to modify it to save the email body content:
global class OrbC_AttachEmailDossier implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
Contact contact = new Contact();
contact.FirstName = email.fromname.substring(0,email.fromname.indexOf(' '));
contact.LastName = email.fromname.substring(email.fromname.indexOf(' '));
contact.Email = envelope.fromAddress;
insert contact;
System.debug('====> Created contact '+contact.Id);
Attachment attachment = new Attachment();
attachment.ParentId = contact.Id;
attachment.Name = email.subject;
attachment.Body = email.HtmlBody;
insert attachment;
return result;
}
}
The problem is that I'm getting this error:
Illegal assignment from String to Blob
When I try to associate the email.htmlBody
to attachment.Body
. What I have to do to get this to work?