0

Is there any way to attach all inbound emails that come from customer to salesforce lead or contact record? right now email to salesforce only works when we send an email to the customer but when they reply back i need that email to be recorded under activity history

Code:

global class AttachEmail 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);

    if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
      for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
        Attachment attachment = new Attachment();
        // attach to the newly created contact record
        attachment.ParentId = contact.Id;
        attachment.Name = email.binaryAttachments[i].filename;
        attachment.Body = email.binaryAttachments[i].body;
        insert attachment;
      }
    }

    return result;

  }

}
1

Using salesforce standard feature it is not possible for you need to incorporate inbound email service class, here is sample code for Inbound email service and using the class you can create the activity history record for that particular Lead record : - please do the below settings changes in your email service :

enter image description here

/**
 * Email services are automated processes that use Apex classes
 * to process the contents, headers, and attachments of inbound
 * email.
 */
global class CreateRecordFrmEmail implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {

        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

        String emailbody = email.body;//you can get email body
        String emailsubject = email.subject;//you can get email Subject
        String subToCompare = 'New user registered';//your own subject

        //put you logic for your three parameters like Name, Position and subject.
        if(email.body.equalsIgnoreCase(subToCompare))
        {

           //insert Activity record based on your code logic.

        }

    result.success = true;
        return result;
    }
}

by this way you can achieve this.

it is working in my DE.

you can test the same by copying the email address in TO or Bcc like below screenshot :

enter image description here

|improve this answer|||||
  • I created apex class using the code i updated in my post and created new email service and added new email address but the replies are not attaching to contact record – Admin_BA May 2 '17 at 19:17
  • please do the changes in your email service as per the screenshots.As you are adding binary attachment so check the "Convert Text attachments to binary attachments " . It will work. – Avijit Das May 2 '17 at 19:53
  • I updated it but how do i test this exactly? I created email service with the apex class and under email address related list, i created one which says "test@lya0ucnsihpmxaai7verl3x208g9i0anzde17bih5uip3fz6o.4b-pjjuau.cs51.apex.sandbox.salesforce.com" and now i created a contact in the sandbox which has an email address "myname@gmail.com". I go to my Gmail and send it to my email address that's associated with my user account and bcc "test@lya0ucnsihpmxaai7verl3x208g9i0anzde17bih5uip3fz6o.4b-pjjuau.cs51.apex.sandbox.salesforce.com" but i don't see any email in the contact related history. – Admin_BA May 2 '17 at 21:02
  • @Admin_BA, have updated my answer with screenshot you test the same like that. – Avijit Das May 3 '17 at 5:12
  • Thank you. I know my code is wrong for my requirement. Right now it's creating contact but actually, i need emails to be recorded under activity history if i receive any from my customer if the email matches with any contact that exist in salesforce, how hard is to change the code? – Admin_BA May 3 '17 at 19:46

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.