+ Start a Discussion
Ram_SF14Ram_SF14 

Whenever a contact is created, an account record needs to created automatically using trigger. Can you please help ?

Best Answer chosen by Ram_SF14
Harshit Garg 6Harshit Garg 6
Hi Ram,

Please folloe the below trigger and test class,

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;
        
    }
    

}

Test 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);
	}
}

Please choose my ans as the best ans.If helpfull for you.

Thanks,
Harshit Garg

All Answers

Harshit Garg 6Harshit Garg 6
Hi Ram,

Please folloe the below trigger and test class,

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;
        
    }
    

}

Test 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);
	}
}

Please choose my ans as the best ans.If helpfull for you.

Thanks,
Harshit Garg
This was selected as the best answer
Harshit Garg 6Harshit Garg 6
Hi Ram,

You can also use that trigger..Both trigger is working great.
 
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.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;
        
    }
    

}
Thanks,
Harshit Garg
 
Ram_SF14Ram_SF14
Thanks Harshit Garg 

Its working fine... I am new to salesforce just wondering is there any other way to write this code in a simpler way...
Harshit Garg 6Harshit Garg 6
Hi Ram,

In this, i have used best practic.but I will give you the simpler way.

If that code helpful for you.Please choose my ans as the best ans.

Thanks,
harshit garg
Ram_SF14Ram_SF14
Hi Harshit 

I have tried to write a handler class for the above trigger for some reasons it is not doing the job. Can you please tweak it to work. 

Thanks 
public class ConUpdateAcc {
    
    public static void creat(List<Contact> con){
        List<Contact> c1 = new List<Contact>();
        for(Contact c2:con){
            if(string.isBlank(c2.accountid)){
                c1.add(c2);
            }
        }
        List<Account> acc = new List<Account>();
        if(c1.size()>0){
            Map<string,Contact> conMap = new Map<string,Contact>();
            for(Contact c3:c1){
                string accountname = c3.LastName;
                conMap.put(accountname, c3);
                Account a1 = new Account();
                a1.Name = accountname;
                acc.add(a1);
            }	insert acc;
        }	
    }

}

 
Harshit Garg 6Harshit Garg 6
Hi ram,

yes, tomorrow I will give you what you want.
Harshit Garg 6Harshit Garg 6
Hi Ram,

Handler class
 
public class CreateContactCreateAccount {

    public static void CreateAccount(list<contact>needAccounts )

    {

    //Collect list of contacts being inserted without an account
    /*for (Contact c : needAccounts ) {
        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.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;
        
    }
    }

}

Trigger
 
trigger CreateContactCreateAccount on Contact (before insert,before update) {
    CreateContactCreateAccount.CreateAccount(trigger.new);

}

If that helpful for you..Please hit the like button.

Thanks,
harshit Garg
Ram_SF14Ram_SF14
Thanks Harshit