Generating an Email-to-Case thread id using Apex

How to generate Email-to-Case thread id inside template:

Generate the thread Id

public static String getThreadId(String caseId){
public static String getThreadId(String caseId){
  return '[ ref:_'
     + UserInfo.getOrganizationId().left(5)
     + UserInfo.getOrganizationId().mid(11,4) + '._'
     + caseId.left(5)
     + caseId.mid(10,5) + ':ref ]';
}

Send customEmail and Add ThreadId

public void sendCustomEmail(User selectedUser, Contact selectedContact, String templateName, Map<String, String> customVariables){
    EmailTemplate emailTemplate = [select Id, Subject, HtmlValue, Body from EmailTemplate where DeveloperName=:templateName];

    if (selectedContact==null){
       selectedContact =  selectedUser.Contact;
    }

    String customSubject = emailTemplate.Subject;

    if (customVariables.containsKey('joinContactName')){
      customSubject = customSubject.replace('{!JointContactName}', customVariables.get('joinContactName'));
    }
    //Add ThreadId to the Subject
    if (customVariables.containsKey('ThreadId')) {
      customSubject = customSubject.replace('{!ThreadId}', customVariables.get('ThreadId'));
    }

    String htmlBody = emailTemplate.HtmlValue;
    htmlBody = htmlBody.replace('{!Contact.FirstName}',selectedContact.FirstName);
    htmlBody = htmlBody.replace('{!Contact.Email}', selectedContact.Email);
    if (customVariables.containsKey('Link')){
      htmlBody = htmlBody.replace('{!CustomUrl}', customVariables.get('Link'));
    } else if (customVariables.containsKey('link')){
      htmlBody = htmlBody.replace('{!CustomUrl}', customVariables.get('link'));
    } else if (customVariables.containsKey('joinContactName')){
      htmlBody = htmlBody.replace('{!JointContactName}', customVariables.get('joinContactName'));
    }

    //Add ThreadId to the htmlBody
    if (customVariables.containsKey('ThreadId')){
      htmlBody = htmlBody.replace('{!ThreadId}', customVariables.get('ThreadId'));
    }

    String plainBody = emailTemplate.Body;
    plainBody = plainBody.replace('{!Contact.FirstName}',selectedContact.FirstName);
    plainBody = plainBody.replace('{!Contact.Email}', selectedContact.Email);
    if (customVariables.containsKey('Link')){
      plainBody = plainBody.replace('{!CustomUrl}', customVariables.get('Link'));
    } else if (customVariables.containsKey('link')){
      plainBody = plainBody.replace('{!CustomUrl}', customVariables.get('link'));
    } else if (customVariables.containsKey('joinContactName')){
      plainBody = plainBody.replace('{!JointContactName}', customVariables.get('joinContactName'));
    }

    //Add the threadId to the plainBody
    if (customVariables.containsKey('ThreadId')){
      plainBody = plainBody.replace('{!ThreadId}', customVariables.get('ThreadId'));
    }

    Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();
    List<OrgWideEmailAddress> orgWideEmailAddress = [Select Id, Address from OrgWideEmailAddress where DisplayName=:ORGWIDEEMAILADDRESSNAME];
    //Set ReplyTo as the Email-to-Case email address
    if (customVariables.containsKey('ThreadId')){
      List<EmailServicesAddress> emailServicesAddress = [SELECT Id,AuthorizedSenders,EmailDomainName,IsActive,LocalPart FROM EmailServicesAddress where IsActive=true and (LocalPart like '%service%' or LocalPart like '%support%')];
      if (!emailServicesAddress.isEmpty()){
        EmailServicesAddress emailToCase = emailServicesAddress.get(0);
        email.setReplyTo(emailToCase.LocalPart +'@'+ emailToCase.EmailDomainName);
        email.setSenderDisplayName('Client Support');
      } else {
        email.setReplyTo('customer-service@gmail.com');
        email.setSenderDisplayName('Client Support');
      }
    }

    email.setTargetObjectId(selectedContact.Id);
    email.setSaveAsActivity(true);

    email.setSubject(customSubject);

    email.setHtmlBody(htmlBody);
    email.setPlainTextBody(plainBody);

    if (customVariables.containsKey('WhatId')){
      email.setWhatId(customVariables.get('WhatId'));
    }

    Messaging.sendEmail(new Messaging.SingleEmailmessage[] {email});
}

Putting it all together

Map<String, String> mappingOfCaseFields = new Map<String, String>();
mappingOfCaseFields.put('ThreadId', App_Service.getThreadId(paymentCaseId));
App_Service.instance.sendCustomEmail(null, contact, 'App_Support_Template', mappingOfCaseFields);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: