Thursday 1 April 2021

Automatically create new contact from Email-To-Case

Requirement : 
While working on one of the requirement of Commerce sector project for a client based out GA, USA; there was a requirement to build support system which can automatically create new case and task for Contact. For new Contact,  it should create the new contact first in Salesforce and then create new case and task under it. 


Challenge : 
Salesforce automatically creates Case from Email. It is called Email-to-Case. This feature also creates tasks along with the Case. Hence the first half of the requirement is solved using the Salesforce standard feature provided by Salesforce Service Cloud. The main challenge for us was to create a new contact whenever we receive an email from a new email address because Salesforce doesn't provide this feature in Email-to-Case. Although, we can install the Email-to-Case Premium package as additional subscription which requires license.

Solution : 
Instead of subscription, We have used the Salesforce Service Cloud provided Email-to-Case feature to create a new case from an e-mail and Apex trigger to create new contact if email address is not exist. 

We can use Email-to-Case in 2 different ways : 
  1. Email-to-Case 
  2. On-demand Email-to-Case
The main difference between these two is Email-to-Case accepts the emails larger than 25 MB while On-Demand Email-to-Case accepts the emails less than 25 MB.

Note : Once you enable Email-to-Case, you cannot disable it. However, you can disable the On-Demand Service.

To setup Email-to-Case service follow the link Email-to-Case. Once setup is completed you will have configuration as shown below.


Now, you also need to configure routing address for the account which is being used as Customer Support in your organization.






Now, you will be having a Salesforce generated dynamic email address that can be used to create the Case.

Since the generated email address is too long to remember, you can configure an email routing address at your support account, so that the email received to your customer support Account, will be forwarded to the Salesforce.

Please refer the Email routing to configure forwarding the email to Salesforce generated dynamic email address. 


For second part of the requirement, to create new contact for the unknown/new email address received by salesforce, we have created a Apex trigger on Case Object. Refer below code for that.  

The trigger has the mechanism to check whether received email address is exist in contacts objects or not.  

If the contact exists with that email address, the case will be assigned to that contact.  

If the contact doesn’t exist, a new contact will be created and then the case will be assigned to that contact. 

Trigger to create contact : 

trigger TriggertoCreateContactforCase on Case (before insert) {
    if(Trigger.isBefore){
        if(Trigger.isInsert){
            List<String> insertedEmailAddresses = new List<String>();
            // Create a list of email addresses for newly inserted case where contact is not set
            for (Case cs:Trigger.new) {
                if (cs.ContactId==null && cs.SuppliedEmail!='' || cs.SuppliedEmail!=null) {
                    insertedEmailAddresses.add(cs.SuppliedEmail);
                }
            }          
            
            Set<String> exstingEmailSet = new Set<String>();
            if(insertedEmailAddresses.size() > 0){
                // Create a set of Email address which are already available in contact
                for (Contact c:[Select Id,Email From Contact Where Email in:insertedEmailAddresses]) {
                    exstingEmailSet.add(c.Email);
                }
            }
            
            Contact contTemp = new Contact();
            List<String> Emailheader = new List<String>();
            List<Case> casesToUpdateList = new List<Case>();
            Map<String,Contact> emailToContactMap = new Map<String,Contact>();
            
            // For all the cases with a null contactId, We will create a contact
            for (Case c:Trigger.new) {
                if (c.ContactId==null && c.SuppliedName!=null && c.SuppliedEmail!=null && c.SuppliedName!='' &&
                    !c.SuppliedName.contains('@') && c.SuppliedEmail!='' && !exstingEmailSet.contains(c.SuppliedEmail)) {
                        Emailheader = c.SuppliedName.split(' ',2);
                        if (Emailheader.size() == 2) {
                            contTemp.FirstName=Emailheader[0];
                            contTemp.LastName=Emailheader[1];
                            contTemp.Email=c.SuppliedEmail;
                            emailToContactMap.put(c.SuppliedEmail,contTemp);
                            casesToUpdateList.add(c);
                        }
                    }
            }
            
            // Inserting Contact
            List<Contact> contactList = new List<Contact>();
            if(emailToContactMap.keyset().size() > 0){
                contactList = emailToContactMap.values();
                insert contactList;
            }
            
            // Updating Cases
            if(contactList.size()>0) {
                for (Case cs:casesToUpdateList) {
                    contTemp = emailToContactMap.get(cs.SuppliedEmail);
                    cs.ContactId = contTemp.Id;
                }
            }
        }
    }
}

Summary : 

By following the above steps, we have achieved the requirement. Now, whenever an email is received to the routing email address, it will automatically create a case and task for that case. While creating the case if the contact is not exist for that email address, the trigger will create a contact and then create a case for that contact. 

If you have any questions you can reach out our Salesforce Consulting team here.

No comments:

Post a Comment