function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bhughesbhughes 

Email-to-case - Automatically creating a new contact?

We have recently been testing out the Email to Case feature and it seems to fit our basic needs for this functionality.

However, I noticed that when someone not logged in Salesforce as a Contact sends us a support request, it creates the case, but it puts absolutely nothing in the contact area, nor does it record the email address that the email was received from.

Is there a way to create a new contact, or at least store the email address it came from so we know who to reply back to?

Thanks for your help!
MarkSilberMarkSilber

With Email2Case, Salesforce does record the email address and name of the person who opened a Case, even if they are not a Contact.

You should have the Web Name, Web Email, Web Company and Web Phone fields available to you on the Case page layout. If they are not showing up, do a search for any of those fields names in the Help & Training section to get more information. You should also check field level security for the profile of the user that is not seeing those fields.

Hope that helps.

BenOrtiz21BenOrtiz21
The Email to Case Premium app can auto create new Contacts for you. It can also associate the newly created contact with an existing Account.

Listing on the AppExchange:  https://appexchange.salesforce.com/listingDetail?listingId=a0N30000001R5cyEAC
Prashant Pandey07Prashant Pandey07
You can you this trigger to create the conact once the case is created from Email...

trigger TriggertoCreateContactformCase on Case (before insert) {
    List<String> UseremailAddresses = new List<String>();
    //First exclude any cases where the contact is set
    for (Case c:Trigger.new) {
        if (c.ContactId==null &&
            c.SuppliedEmail!=''|| c.SuppliedEmail==null)
        {
            UseremailAddresses.add(c.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> listofallContacts = [Select Id,Email From Contact Where Email in:UseremailAddresses];
    Set<String> ExstingEmails = new Set<String>();
    for (Contact c:listofallContacts) {
        ExstingEmails.add(c.Email);
    }
    
    Map<String,Contact> emailToContactMap = new Map<String,Contact>();
    List<Case> casesToUpdate = new List<Case>();

    for (Case c:Trigger.new) {
        if (c.ContactId==null &&
            c.SuppliedName!=null &&
            c.SuppliedEmail!=null &&
            c.SuppliedName!='' &&
           !c.SuppliedName.contains('@') &&
            c.SuppliedEmail!='' &&
           !ExstingEmails.contains(c.SuppliedEmail))
        {
            //The case was created with a null contact
            //Let's make a contact for it
            String[] Emailheader = c.SuppliedName.split(' ',2);
            if (Emailheader.size() == 2)
            {
                Contact conts = new Contact(FirstName=Emailheader[0],
                                            LastName=Emailheader[1],
                                            Email=c.SuppliedEmail
                                            );
                emailToContactMap.put(c.SuppliedEmail,conts);
                casesToUpdate.add(c);
            }
        }
    }
    
    List<Contact> newContacts = emailToContactMap.values();
    insert newContacts;
    
    for (Case c:casesToUpdate) {
        Contact newContact = emailToContactMap.get(c.SuppliedEmail);
        
        c.ContactId = newContact.Id;
    }
}
Josh ThiessenJosh Thiessen
Does anyone know how to convert the about Apex to be for Person accounts instead of contacts?
AkshayNaidu07AkshayNaidu07
@Prashant Pandey07
The Code works great!
However, I am unable to deploy the code from Sandbox to Production. As it only provided code coverage of 54%. Is there anyway to increase the code coverge.
Thanks!