• Randa Green
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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;
    }
}