+ Start a Discussion
Mahesh Babu 187Mahesh Babu 187 

Auto-create a new account and contact through web-to-case

Hi Team,
I have a requirement to auto-create an account and contact through web-to-case. I am able to create contact through trigger if the email id doesn't exists.As the contact is created it becomes private conatct because it not associated to any account. So I need to create the account from 'Suppliedcompany' field and map this account to the contact.

APEX CODE

trigger CreateContact on Case (before insert) {
    
        List<String> emailAddresses = new List<String>();
    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null && caseObj.SuppliedEmail!='')
        {
            emailAddresses.add(caseObj.SuppliedEmail);             
        }
    }
    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.SuppliedName!='' && caseObj.SuppliedEmail!=null &&
            caseObj.SuppliedEmail!='' && caseObj.SuppliedPhone!=null && caseObj.SuppliedPhone!='' && !takenEmails.contains(caseObj.SuppliedEmail))
         {
            String[] nameParts = caseObj.SuppliedName.split(' ',2);
            if (nameParts.size() == 2)
            {
             Contact cont = new Contact(FirstName=nameParts[0], LastName=nameParts[1], Email=caseObj.SuppliedEmail, Phone=caseObj.SuppliedPhone);
                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;
    }
}

Thanks,
Mahesh
Maharajan CMaharajan C
HI Mahesh,

Try the below code:
 
trigger CreateContact on Case (before insert) {
    
        List<String> emailAddresses = new List<String>();
    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null && caseObj.SuppliedEmail!='')
        {
            emailAddresses.add(caseObj.SuppliedEmail);             
        }
    }
    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>();
	Map<String,Account> emailToAccountMap = new Map<String,Account>();
    List<Case> casesToUpdate = new List<Case>();
    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null && caseObj.SuppliedName!=null && caseObj.SuppliedName!='' && caseObj.SuppliedEmail!=null &&
            caseObj.SuppliedEmail!='' && caseObj.SuppliedPhone!=null && caseObj.SuppliedPhone!='' && !takenEmails.contains(caseObj.SuppliedEmail))
         {
            String[] nameParts = caseObj.SuppliedName.split(' ',2);
            if (nameParts.size() == 2)
            {
				if(caseObj.Suppliedcompany != null && caseObj.Suppliedcompany != null)
				{
					// Add the remaining mandatory fields for Account.
					Account acc = new Account(Name = caseObj.Suppliedcompany);
					emailToAccountMap.put(caseObj.SuppliedEmail, acc);
				}
				
				Contact cont = new Contact(FirstName=nameParts[0], LastName=nameParts[1], Email=caseObj.SuppliedEmail, Phone=caseObj.SuppliedPhone);
                emailToContactMap.put(caseObj.SuppliedEmail,cont);
                casesToUpdate.add(caseObj);
            }
        }
    }
	
	if(!emailToAccountMap.IsEmpty())
		insert emailToAccountMap.values();
	
	for(Contact con : emailToContactMap.values()){
		if(emailToAccountMap.containskey(con.Email)){
			con.AccountId = emailToAccountMap.get(con.Email).Id;
		}
	}
	if(!emailToContactMap.IsEmpty())
		insert emailToContactMap.values();

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

Thanks,
Maharajan.C