I am using email to case and it works really well. I created a trigger to create a contact if it already doesn't exist. My issue is when the trigger obtains the email address, it saves information that isn't regularly in my email address. The regular address is [email protected]. The trigger saves [email protected]. This essentially creates a new contact. Does anyone know how to fix this. Here is the snippet where the contact is created:

contact creating part of trigger.

trigger EmailCaseCreateContactTrigger on Case (before insert) {
List<String> emailAddresses = new List<String>();
//Exclude cases where contact is set
for (Case caseObj :Trigger.new) {
    if(caseObj.ContactId == null && caseObj.SuppliedEmail != '') {
//Query to see if the 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) {

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)) {

            //Create contact for case that was created with a null contact.
            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,
                                          Business_ID__c = '0000');



List<Contact> newContacts = emailToContactMap.values();
insert newContacts;

for (Case caseObj :casesToUpdate) {
    Contact newContact = emailToContactMap.get(caseObj.SuppliedEmail);

    caseObj.ContactId = newContact.Id;


  • This code cannot have that effect, so we have to assume that there is some other code that has that effect. Can you provide the rest of the trigger? Are there any other triggers or workflows firing?
    – user735
    Feb 4, 2015 at 23:11
  • @JohnWestenhaver I added the entire trigger to the question above. There is another trigger, but It just queries up our hierarchy to autopopulate a field on case. I had another person in the company send an email to fire this trigger, and his email didn't have the .onmicrosoft.com so I contacted our tech support.
    – kwiknick
    Feb 4, 2015 at 23:23
  • n.b - (1) you can simplify your code a bit by replacing the tests for null and empty string with !String.isEmpty(caseObj.SuppliedName) and (2) if the supplied name is something like Dick Van Dyke, then you'll not create a Contact. Same problem if the supplied name is Bono or Prince.
    – cropredy
    Feb 5, 2015 at 0:29

1 Answer 1


Fortunately my issue had nothing to do with my code, and was an issue with the way my email was set up. Thanks to all who commented I truly appreciate the help.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .