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
pierrefrazny.ax358pierrefrazny.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 

Best Answer chosen by Admin (Salesforce Developers) 
geetageeta

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

crmexpertcrmexpert

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!!

pierrefrazny.ax358pierrefrazny.ax358

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

 

 

 Thanks

 

Message Edited by pierrefrazny on 06-01-2009 06:31 AM
Message Edited by pierrefrazny on 06-01-2009 06:31 AM
BritishBoyinDCBritishBoyinDC

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... 

Message Edited by BritishBoyinDC on 06-02-2009 08:19 PM
pierrefrazny.ax358pierrefrazny.ax358

I am creating a new account for each new contact. This is where I having difficulties. Any help/guidance?

Thanks

Pierre 

BritishBoyinDCBritishBoyinDC
The non-profit group use that model and have some code that does it...try posting something on the non-profit discussion board, or at the NP community (http://groups.google.com/group/npsf) 
geetageeta

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

This was selected as the best answer
pierrefrazny.ax358pierrefrazny.ax358

Geeta,

Thanks. This works and is very helpful. I will experiment and report back if I have issues/.

Thanks!

Pierre 

pierrefrazny.ax358pierrefrazny.ax358

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 

 

 

Message Edited by pierrefrazny on 06-05-2009 05:45 PM