2

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?

2

The error says you are trying to assign a String value (email.HtmlBody) to a Blob type field/variable (attachment.Body).

You can convert the String into a Blob and then assign it.

attachment.Body = Blob.valueOf(email.HtmlBody);

OR

you can follow the examples in this developer force reference and create a note from the email text body

Note note = new Note();    
note.Title = email.fromName + ' (' + DateTime.now() + ')';
note.Body = email.plainTextBody;
note.ParentId = account.Id;
insert note;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.