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
Dhiraj Kumar 26Dhiraj Kumar 26 

Insert Parent(Account) on creation of Child(Contact) Record

If a contact is not associated with any of the account, I want to create an Account via Trigger . 
Basically I want three operations:
1. Insert a Contact record
2. Then Account will be inserted
3. Then link contact to Account 


This is my code
trigger createAccount on Contact (before insert) {

List<Contact> conlist = new List<Contact>();

    for(Contact c : Trigger.New)
        {
            if(c.accountId == null)
            {
                conlist.add(c);
            }
        }
    if(conList.size()>0)
        {
            List<Account> acclist = new List<Account>();
            Map<String,Contact> conmap = new map<String,Contact>();
            
            for(Contact c : conList)
            {
                String accountName = c.firstname+' '+c.lastname;
                Account a = new Account(name=accountname);
                accList.add(a);
            }
          insert accList;
          
              for(Account a : acclist)
              {
                  if(conmap.containskey(a.name))
                  {
                      conmap.get(a.name).accountId = a.id;
                  }
              }
        }

}
Nishad KNishad K
Hi,
try the below trigger, 
 
trigger createAccount on Contact (before insert, before update) {

  Map<Contact, Account> con_acc_map = new Map<Contact, Account>();
  List<Account> list_acc = new List<Account>();

  for (Contact c : trigger.new){
    Account acc = new Account();
    if(c.AccountID == null  ){
        string cName = c.firstname+' '+c.lastname;
        acc.name = cName;
        con_acc_map.put(c, acc);

    }
  }

  insert con_acc_map.values();

  //update the contacts' account field
  for (Contact c : trigger.new){
    Account a = con_acc_map.get(c); 
    c.AccountId = a.id;
  }

}
Regards,
 
Amit Singh 1Amit Singh 1
Ok, Use below code.
 
trigger createAccount on Contact (before insert) {
List<Account> acclist = new List<Account>();
Map<String,Id> conmap = new map<String,Id>();
List<Contact> conlist = new List<Contact>();
for(Contact c : Trigger.New){
        if(c.accountId == null){
            conlist.add(c);
        }
    }
    if(conList.size()>0){           
            for(Contact c : conList){
                String accountName = c.firstname+' '+c.lastname;
                Account a = new Account(name=accountname);
                accList.add(a);
            }
            insert accList;
            for(Account a : acclist){
                conmap.put(a.Name,a.Id);  
            }
            For(Contact con :conList){
                if(conmap.containsKey(con.firstname+' '+con.lastname)){
                    con.AccountId = conmap.get(con.firstname+' '+con.lastname);
                }
            }
    }

}

Let us know if this helps :)

Thanks,
Amit Singh