Autocreating Contacts From Web To Case

Contents

Quickstart: Autocreating Contacts From Web To Case

This is part of the Developing with Service & Support Quickstarts

Use Case

Normally, Web To Case and Email To Case do not create contacts for incoming web cases and emails -- they only associate the cases to existing contacts. There are some scenarios where people may want to autocreate contacts, however. This quickstart discusses how to do that.

See the blog post corresponding to this quick start for more information.

Sample Code: Autocreate Contact Trigger

This trigger creates contacts if a case created by Web To Case or Email To Case is not associated with a contact.

trigger CaseAutocreateContact on Case (before insert) {
    List<String> emailAddresses = new List<String>();
    //First exclude any cases where the contact is set
    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null &&
            caseObj.SuppliedEmail!='')
        {
            emailAddresses.add(caseObj.SuppliedEmail);
        }
    }

    //Now we have a nice list of all the email addresses.  Let's query on it and see how many contacts already exist.
    List<Contact> listContacts = [Select Id,Email From Contact Where Email in :emailAddresses];
    Set<String> takenEmails = new Set<String>();
    for (Contact c:listContacts) {
        takenEmails.add(c.Email);
    }
    
    Map<String,Contact> emailToContactMap = new Map<String,Contact>();
    List<Case> casesToUpdate = new List<Case>();

    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null &&
            caseObj.SuppliedName!=null &&
            caseObj.SuppliedEmail!=null &&
            caseObj.SuppliedName!='' &&
            !caseObj.SuppliedName.contains('@') &&
            caseObj.SuppliedEmail!='' &&
            !takenEmails.contains(caseObj.SuppliedEmail))
        {
            //The case was created with a null contact
            //Let's make a contact for it
            String[] nameParts = caseObj.SuppliedName.split(' ',2);
            if (nameParts.size() == 2)
            {
                Contact cont = new Contact(FirstName=nameParts[0],
                                            LastName=nameParts[1],
                                            Email=caseObj.SuppliedEmail,
                                            Autocreated__c=true);
                emailToContactMap.put(caseObj.SuppliedEmail,cont);
                casesToUpdate.add(caseObj);
            }
        }
    }
    
    List<Contact> newContacts = emailToContactMap.values();
    insert newContacts;
    
    for (Case caseObj:casesToUpdate) {
        Contact newContact = emailToContactMap.get(caseObj.SuppliedEmail);
        
        caseObj.ContactId = newContact.Id;
    }
}

Sample Code: Test class for the above trigger

public class CaseAutocreateContactTest {
    
    public static testMethod void testBulkContactsGetCreated() {
        List<Case> newCases = new List<Case>();
        for (Integer i = 0; i<100; i++) {
            Case c = new Case(SuppliedEmail='jdoe_test_test@doe.com' + i,
                                SuppliedName='John Doe' + i,
                                Subject='Feedback - Something' + i);
            newCases.add(c);
        }
        insert newCases;
        
        System.debug('here');
        List<Id> newCaseIds = new List<Id>();
        for (Case caseObj:newCases) {
            newCaseIds.add(caseObj.Id);    
        }
        
        List<Case> updatedCases = [Select ContactId From Case Where Id in :newCaseIds];
        
        for (Case caseObj:updatedCases) {
            System.debug(caseObj.Id + ' ' + caseObj.ContactId);
            System.assert(caseObj.ContactId!=null,'There should be no null contacts');
        }
    }

    public static testMethod void testContactGetsCreated() {
        Case c = new Case(SuppliedEmail='jdoe_test_test@doe.com',
                            SuppliedName='John Doe',
                            Subject='Feedback - Something');
        insert c;

        List<Contact> johnDoes = [select Id from Contact where Email='jdoe_test_test@doe.com'];

        //there should be only 1 -- the trigger should not have created another
        System.assert(johnDoes.size()==1, 'There should be one John Doe!');
        
        Case caseObj = [select ContactId from Case where Id=:c.Id];
        System.assert(caseObj.ContactId!=null,'There should be no null contact on the case');
    }

    public static testMethod void testNoDupesAreCreated() {
        Contact cnt1 = new Contact(FirstName = 'John',
                                LastName = 'Doe',
                                Email='jdoe_test_test@doe.com');

        insert cnt1;

        Case case1 = new Case(SuppliedEmail='jdoe_test_test@doe.com',
                            SuppliedName='John Doe',
                            Subject='Feedback - Something');

        insert case1;

        List<Contact> johnDoes = [select Id from Contact where Email='jdoe_test_test@doe.com'];

        //there should be only 1 -- the trigger should not have created another
        System.assert(johnDoes.size()==1, 'There should be only one John Doe!');
    }

    public static testMethod void testEmailNameDoesntGetCreated() {
        Case c = new Case(SuppliedEmail='testEmailNameDoesntGetCreated@doe.com',
                            SuppliedName='testEmailNameDoesntGetCreated@doe.com',
                            Subject='Feedback - Something');
        insert c;

        List<Contact> johnDoes = [select Id from Contact where Email='testEmailNameDoesntGetCreated@doe.com'];

        //there should be only 1 -- the trigger should not have created another
        System.assert(johnDoes.size()==0, 'There should be no John Does!');
    }
}