You need to sign in to do that
Don't have an account?
pierrefrazny.ax358
Account creation automation
Hello,
I would like to automate the creation of an Account when a Contact is created and the Account Name is left blank. Do I need Visual Force for this or is an Apex trigger sufficient? Could someone put me in the right direction?
Thanks
Pierre
try the code below:
trigger createAcc on Contact (before insert) {
List<Account> accList = new List<Account>();
List<Contact> conList = new List<Contact>();
for (Contact c : Trigger.new) {
if (c.AccountId == null) {
Account a = new Account(Name = c.FirstName + c.LastName);
accList.add(a);
conList.add(c);
}
}
List<String> ids = new List<String>();
if (accList.size() > 0) {
Database.SaveResult[] result = Database.Insert(accList, false);
for (Database.SaveResult sr : result){
ids.add(sr.Id);
}
}
if (ids.size() > 0) {
for (Integer i=0; i<ids.size(); i++) {
Contact c = conList.get(i);
if (ids.get(i) != null) {
c.AccountId = ids.get(i);
}
}
}
}
All Answers
An After Insert Trigger On Contact would be more than enough. no need of a vf page.
Did it with ease. Please get back in case of doubts..
Thanks!!
Hi,
Thanks for the answer. I started writting the trigger. I think I have the portion to create the Accounts but I don't understand how to attach the Contact and Account once the Accounts have been created.
Here is what I have so far:
trigger Account on Contact (after insert) {
Map<ID, Contact> conts = new Map<ID, Contact>();
if(Trigger.isInsert) {
// build map of Contacts with no Account
for(Contact c : System.Trigger.new){
if(c.Account == NULL){
conts.put(c.Id,c);
}
}
Account[] Accounts = new List<Account>();
for (Contact c : [SELECT FirstName, LastName FROM Contact Where Id IN :conts.KeySet()]) {
Account a = new Account(Name = c.FirstName + c.LastName, Accounts.add(a));
}
insert Accounts;
}
}
Just to check - are you setting the account to a single, default account id, or are you creating a new account for each contact, and then associating the contact to that new account?
The former is obviously much simpler - you can do that in the before insert trigger and just set it to an id; the latter is harder, and would probably need something like you are coding...
I am creating a new account for each new contact. This is where I having difficulties. Any help/guidance?
Thanks
Pierre
try the code below:
trigger createAcc on Contact (before insert) {
List<Account> accList = new List<Account>();
List<Contact> conList = new List<Contact>();
for (Contact c : Trigger.new) {
if (c.AccountId == null) {
Account a = new Account(Name = c.FirstName + c.LastName);
accList.add(a);
conList.add(c);
}
}
List<String> ids = new List<String>();
if (accList.size() > 0) {
Database.SaveResult[] result = Database.Insert(accList, false);
for (Database.SaveResult sr : result){
ids.add(sr.Id);
}
}
if (ids.size() > 0) {
for (Integer i=0; i<ids.size(); i++) {
Contact c = conList.get(i);
if (ids.get(i) != null) {
c.AccountId = ids.get(i);
}
}
}
}
Geeta,
Thanks. This works and is very helpful. I will experiment and report back if I have issues/.
Thanks!
Pierre
I also wrote the after delete / after update trigger:
trigger DeletionHousehold on Contact (after delete, after update)
{
if( Trigger.IsUpdate || Trigger.IsDelete){
List<Account> accList = new List<Account>();
Set<Id> accIdSet = new Set<Id>();
for (Contact c : Trigger.old) { accIdSet.add(c.AccountId); }
for (Account a : [select Id, RecordType.Name,(select Id from Contacts) from Account where Id in :accIdSet])
{ accList1.add(a); }
List<Account> delaccList = new List<Account>();
for (Account a: accList){
if (a.Contacts.size()==0){ delaccList.add(a); }
}
if (delaccList.size()>0){ delete delaccList; }
}
}
Thanks again for your precious help.
Pierre