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.
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.
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;
}
}
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.
Please folloe the below trigger and test class,
Trigger
Test class
Please choose my ans as the best ans.If helpfull for you.
Thanks,
Harshit Garg
All Answers
Please folloe the below trigger and test class,
Trigger
Test class
Please choose my ans as the best ans.If helpfull for you.
Thanks,
Harshit Garg
You can also use that trigger..Both trigger is working great.
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...
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
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
yes, tomorrow I will give you what you want.
Handler class
Trigger
If that helpful for you..Please hit the like button.
Thanks,
harshit Garg