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
srikanth cheerasrikanth cheera 

whenever contact is created create account with the contact lastname

Khan AnasKhan Anas (Salesforce Developers) 
Hi Srikanth,

Greetings to you.

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger InsertAccountIfContactCreated on Contact (before insert) {
    
    List<Contact> needAccounts = new List<Contact>();
    for (Contact c : trigger.new) {
        needAccounts.add(c);
    }
    
    if (needAccounts.size() > 0) {
        List<Account> newAccounts = new List<Account>();
        for (Contact c : needAccounts) {
            String accountName = c.lastname;
            Account a = new Account(name=accountName);
            newAccounts.add(a);
        }
        INSERT newAccounts;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
Jolly_BirdiJolly_Birdi
Hello @Srikanth

Please add this below Code in your Contact trigger after insert:

List<Account> newAccounts = new List<Account>();
for (Contact c : trigger.new) {
        newAccounts.add(new Account(name=c.lastname));
}

if(!newAccounts.isEmpty()){
        insert newAccounts;
}


Please mark it as best answer if you find it positive.

Thanks,
Jolly Birdi

 
Gulafsha MohammedGulafsha Mohammed
Hello Srikanth,
You need after trigger to update on another object.
trigger InsertAccountIfContactCreated on Contact (after insert) {
    
    List<Contact> needAccounts = new List<Contact>();
    for (Contact c : trigger.new) {
        needAccounts.add(c);
    }
    
    if (needAccounts.size() > 0) {
        List<Account> newAccounts = new List<Account>();
        for (Contact c : needAccounts) {
            String accountName = c.lastname;
            Account a = new Account(name=accountName);
            newAccounts.add(a);
        }
        INSERT newAccounts;
    }
}
Hope this will help you.Please mark it as best answer if you find it positive.
Regards,
Gulafsha
 
Ajay K DubediAjay K Dubedi
Hi Srikanth,

Below code can fulfill your requirements. Hope this will work for you.

Trigger :

trigger CreateDefaultContact on Contact (after insert) {
    
   ContactDefault.createContact(trigger.new);

}


Class:

public class ContactDefault {
    
    public static void createContact(List<Contact> contactList)
    {
       
            List<Account> accountList = new List<Account>();
            
            for(Contact conObj : contactList)
            {
                Account accObj = new Account();
                accObj.Name = conObj.LastName;
                accountList.add(accObj);
            }
            
            if(accountList.size() > 0)
            {
                insert accountList;
                
            }
        
    }
    
}

Please mark this as best answer if this solves your problem.

Thank you
Ajay Dubedi
 
Raj VakatiRaj Vakati
Hi Khan Anas  & Ajay Dubedi , 

Your code is correct but one key point is missing .. 

You create an an Account from the contact  .. But forget to link the account and contact relation ..  In Salesforce isolcated contacts have some limitations ,, 



srikanth cheera​ 

Bulkfiy this code 
trigger InsertAccountIfContactCreated on Contact (after insert) {
    
    List<Contact> needAccounts = new List<Contact>();
    for (Contact c : trigger.new) {
        needAccounts.add(c);
    }
    
    if (needAccounts.size() > 0) {
        //List<Account> newAccounts = new List<Account>();
        for (Contact c : needAccounts) {
            String accountName = c.lastname;
            Account a = new Account(name=accountName);
			insert a ; 
            //newAccounts.add(a);
			c.AccountId = a.Id ;
        }
       // INSERT newAccounts;
		 
		
		
    }
}




 
Rameshvar Dayal 48Rameshvar Dayal 48

Here is the answer for your ask

trigger contactTrigger on Contact(after insert) {
    Map<Id, Account> contactAccountMap = new Map<Id, Account>();
    List<Contact> contacts = trigger.new;

    for(Contact con :contacts) {
        Account ac = new Account()
        ac.Name = 'Test';
        contactAccountMap.put(con.Id, ac);
    }

    if(contactAccountMap.size() > 0)
        insert contactAccountMap.values();


    for(Contact con :contacts) {
        Account ac = contactAccountMap.get(con.id);
        con.accountId = ac.id
    }

    update contacts;
}
VenkatramanVenkatraman
Try this one:
 
trigger conAccount on Contact (before insert) {
    Map<String,Contact> conMap = new Map<String,Contact>();
    List<Account> acc = new List<Account>();
    for(Contact con:trigger.new){
        //Only create the account record when a contact is not linked to account
        if(String.isBlank(con.accountId)){
            conMap.put(con.LastName, con);
            acc.add(new Account(name=con.LastName));
        }
    }
    if(acc.size()>0){
        insert acc;
        for(Account a : acc){            
            if(conMap.containsKey(a.Name)){
                conMap.get(a.Name).accountId = a.Id; 
            }
        }
    }
}