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
Rob LilleyRob Lilley 

Trigger to automatically create account record when creating a contact record

Hi, I wonder if anyone can help please? I know only enough about Apex to create a trigger and class but not to write the code...
I would the ability to automatically create an Account Record when entering a new Contact. (Account Name based on the Last Name of the Contact). Ideally the Account Name to be in the format "The " & Last_Name_& " Household"

(Please note Accounts renamed to 'Households' & Contacts to 'Persons')

I copied and tried this Trigger:

trigger CreateAccountFromContact on Contact (before insert) {
    //Collect list of contacts being inserted without an account
    List<Contact> needAccounts = new List<Contact>();
    for (Contact c : trigger.new) {
        if (String.isBlank(c.accountid)) {
            needAccounts.add(c);
        }
    }
    
    if (needAccounts.size() > 0) {
        List<Account> newAccounts = new List<Account>();
        Map<String,Contact> contactsByNameKeys = new Map<String,Contact>();
        //Create account for each contact
        for (Contact c : needAccounts) {
            String accountName = c.firstname + ' ' + c.lastname;
            contactsByNameKeys.put(accountName,c);
            Account a = new Account(name=accountName);
            newAccounts.add(a);
        }
        insert newAccounts;
        
        //Collect a new list of deal__c objects for new accounts
        //List<Deal__c> newRecsForAccounts = new List<Deal__c>();
        for (Account a : newAccounts) {
            //Put account ids on contacts
            if (contactsByNameKeys.containsKey(a.Name)) {
                contactsByNameKeys.get(a.Name).accountId = a.Id;
            }
            //Deal__c newRec = new Deal__c(name=a.Name, accountId=a.Id);
        }
        
        //insert newRecsForAccounts;
        
    }
    
With this Class:
@isTest
public with sharing class CreateAccountFromContact_UnitTest {
    @isTest
    public static void runTest(){
        String firstname = 'first';
        String lastname = 'last';
        String email = 'firstlast@test.com';
        
        //Create contact
        Contact c = new Contact(firstname=firstname, lastname=lastname, email=email);
        insert c;
        
        //Verify account
        c = [select id, accountid, firstname, lastname, email from Contact where id =:c.Id][0];
        Account a = [select id, name from Account where id = :c.accountId][0];
        
        system.assertEquals(firstname + ' ' + lastname, a.Name);
    }
}
The Class passes the test ok and trigger is active but when I try to save the Contact record the system issues the message:

Contact Creation
I was hoping not to create the Account first,

Many thanks for any help, Rob 
Best Answer chosen by Rob Lilley
stsramstsram
I think you have set Account Name required in the Page Layout
Or have a lookup filter condition for which the  new created  account doesnt satisfy
 

All Answers

stsramstsram
I think you have set Account Name required in the Page Layout
Or have a lookup filter condition for which the  new created  account doesnt satisfy
 
This was selected as the best answer
Rob LilleyRob Lilley
Hi Stsram,

Perfect, thank you. The required field was on teh Account name in the Contact layout.

best wishes, Rob