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
IPFramptonIPFrampton 

Create Person Account after Web-To-Case

Hiya,

I am trying to create a Person Account when a Web-To-Case cannot find a matching account relating to the email address specified in the form we have created. I have taken the code which is provided at this link to create the Person Account: https://developer.salesforce.com/page/Autocreating_Contacts_From_Web_To_Case however, this does not also create the Contact automatically. Is someone able to point me in the right direction to be able to create the Contact element and be able to map this back to the Case?

The code I have is below:
 
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);
            System.Debug('>>> the value of the supplied email is ' + caseObj.SuppliedEmail);
        }
    }
    System.Debug('>>> the size of the email address object is ' + emailAddresses.size());
    
    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,Account> emailToAccountMap= new Map<String,Account>();
    Map<String,Contact> emailToContactMap= new Map<String,Contact>();
    List<Case> casesToUpdate = new List<Case>();
 
    for (Case caseObj:Trigger.new) {
            System.Debug('>>> the value of the supplied email is ' + caseObj.SuppliedEmail);
            System.Debug('>>> the value of the first name is ' + caseObj.First_Name__c);
            System.Debug('>>> the value of the last name is ' + caseObj.Last_Name__c );
            System.Debug('>>> the value of the supplied ContactID is ' + caseObj.ContactId);    
        if (caseObj.ContactId==null && caseObj.SuppliedEmail!=null && caseObj.Last_Name__c != '' && caseObj.Last_Name__c != null && caseObj.SuppliedEmail!='' &&
            !takenEmails.contains(caseObj.SuppliedEmail))
        {
            //The case was created with a null contact
            //Let's make a contact for it
           RecordType personAccountRecordType = [SELECT Id FROM RecordType WHERE Name = 'Customer' and SObjectType = 'Account'];
           Account newPersonAccount = new Account(FirstName= caseObj.First_Name__c,
                                            LastName= caseObj.Last_Name__c,
                                            E_mail_address__c =caseObj.SuppliedEmail,
                                            RecordType=personAccountRecordType );
           
            emailToAccountMap.put(caseObj.SuppliedEmail,newPersonAccount);
            casesToUpdate.add(caseObj);
         }
    }
    List<Account> new Accounts = emailToAccountMap.values();
    insert newAccounts;

    for (Case caseObj:casesToUpdate) {
        Account newPersonAccount = emailToAccountMap.get(caseObj.SuppliedEmail);
        caseObj.AccountId = newPersonAccount.Id;
        System.Debug('>>> the person contact id of the person object is ' + newPersonAccount.PersonContactId);
        caseObj.ContactId = newPersonAccount.PersonContactId;
    }
}

 
Best Answer chosen by IPFrampton
Vishal_GuptaVishal_Gupta
Hi IP,

Please use the below code and let me know if it will work for you, I have bold the statment which I have added or updated :

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);
            System.Debug('>>> the value of the supplied email is ' + caseObj.SuppliedEmail);
        }
    }
    System.Debug('>>> the size of the email address object is ' + emailAddresses.size());
    
    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,Account> emailToAccountMap= new Map<String,Account>();
    Map<String,Contact> emailToContactMap= new Map<String,Contact>();
    List<Case> casesToUpdate = new List<Case>();
    RecordType personAccountRecordType = [SELECT Id FROM RecordType WHERE Name = 'Customer' and SObjectType = 'Account'];
    for (Case caseObj:Trigger.new) {
            System.Debug('>>> the value of the supplied email is ' + caseObj.SuppliedEmail);
            System.Debug('>>> the value of the first name is ' + caseObj.First_Name__c);
            System.Debug('>>> the value of the last name is ' + caseObj.Last_Name__c );
            System.Debug('>>> the value of the supplied ContactID is ' + caseObj.ContactId);    
        if (caseObj.ContactId==null && caseObj.SuppliedEmail!=null && caseObj.Last_Name__c != '' && caseObj.Last_Name__c != null && caseObj.SuppliedEmail!='' &&
            !takenEmails.contains(caseObj.SuppliedEmail))
        {
            //The case was created with a null contact
            //Let's make a contact for it
           
           Account newPersonAccount = new Account(FirstName= caseObj.First_Name__c,
                                            LastName= caseObj.Last_Name__c,
                                            E_mail_address__c =caseObj.SuppliedEmail,
                                            RecordType=personAccountRecordType );
           
            //emailToAccountMap.put(caseObj.SuppliedEmail,newPersonAccount);
            casesToUpdate.add(caseObj);
         }
    }
    
    List<Account> new Accounts = emailToAccountMap.values();
    insert newAccounts;
    Set<Id> accIds = new Set<Id>();
    for(Account prsnAcc : newAccounts)
    {
        accIds.add(prsnAcc.id);
    }
    Map<String,Account> mapPrsnAccounts = new Map<String,Account>();
    for(Account prsnAcc : [select id,PersonContactId,E_mail_address__c from Account where Id in : accIds])
    {
        mapPrsnAccounts.put(prsnAcc.E_mail_address__c,prsnAcc);
    }


    for (Case caseObj:casesToUpdate) {
        Account newPersonAccount = mapPrsnAccounts.get(caseObj.SuppliedEmail);
        caseObj.AccountId = newPersonAccount.Id;
        System.Debug('>>> the person contact id of the person object is ' + newPersonAccount.PersonContactId);
        caseObj.ContactId = newPersonAccount.PersonContactId;
    }
}

Thanks,
Vishal

All Answers

Vishal_GuptaVishal_Gupta
Hi,

You need to queried PersonContactId from the newly inserted Person Accounts like :

Select Id,PersonContactId from account where Id In : newAccounts

after that you can set caseObj.ContactId = newPersonAccount.PersonContactId;

I also found that following query is in loop which we can keep outside :

RecordType personAccountRecordType = [SELECT Id FROM RecordType WHERE Name ='Customer' and SObjectType = 'Account'];

Please let me know if more input require.

Thanks,
Vishal
IPFramptonIPFrampton
Hi Vishal. Where would I put this lookup in the code above? (The ContactID lookup that is). Also, would I need to an additional map from the Case object to the Contact as well? Sorry I'm very new to Apex.
Vishal_GuptaVishal_Gupta
Hi IP,

Please use the below code and let me know if it will work for you, I have bold the statment which I have added or updated :

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);
            System.Debug('>>> the value of the supplied email is ' + caseObj.SuppliedEmail);
        }
    }
    System.Debug('>>> the size of the email address object is ' + emailAddresses.size());
    
    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,Account> emailToAccountMap= new Map<String,Account>();
    Map<String,Contact> emailToContactMap= new Map<String,Contact>();
    List<Case> casesToUpdate = new List<Case>();
    RecordType personAccountRecordType = [SELECT Id FROM RecordType WHERE Name = 'Customer' and SObjectType = 'Account'];
    for (Case caseObj:Trigger.new) {
            System.Debug('>>> the value of the supplied email is ' + caseObj.SuppliedEmail);
            System.Debug('>>> the value of the first name is ' + caseObj.First_Name__c);
            System.Debug('>>> the value of the last name is ' + caseObj.Last_Name__c );
            System.Debug('>>> the value of the supplied ContactID is ' + caseObj.ContactId);    
        if (caseObj.ContactId==null && caseObj.SuppliedEmail!=null && caseObj.Last_Name__c != '' && caseObj.Last_Name__c != null && caseObj.SuppliedEmail!='' &&
            !takenEmails.contains(caseObj.SuppliedEmail))
        {
            //The case was created with a null contact
            //Let's make a contact for it
           
           Account newPersonAccount = new Account(FirstName= caseObj.First_Name__c,
                                            LastName= caseObj.Last_Name__c,
                                            E_mail_address__c =caseObj.SuppliedEmail,
                                            RecordType=personAccountRecordType );
           
            //emailToAccountMap.put(caseObj.SuppliedEmail,newPersonAccount);
            casesToUpdate.add(caseObj);
         }
    }
    
    List<Account> new Accounts = emailToAccountMap.values();
    insert newAccounts;
    Set<Id> accIds = new Set<Id>();
    for(Account prsnAcc : newAccounts)
    {
        accIds.add(prsnAcc.id);
    }
    Map<String,Account> mapPrsnAccounts = new Map<String,Account>();
    for(Account prsnAcc : [select id,PersonContactId,E_mail_address__c from Account where Id in : accIds])
    {
        mapPrsnAccounts.put(prsnAcc.E_mail_address__c,prsnAcc);
    }


    for (Case caseObj:casesToUpdate) {
        Account newPersonAccount = mapPrsnAccounts.get(caseObj.SuppliedEmail);
        caseObj.AccountId = newPersonAccount.Id;
        System.Debug('>>> the person contact id of the person object is ' + newPersonAccount.PersonContactId);
        caseObj.ContactId = newPersonAccount.PersonContactId;
    }
}

Thanks,
Vishal
This was selected as the best answer
Randa GreenRanda Green
Hi - What was the final code that worked for this person account creation?