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
AdhyaAdhya 

I am newbie in salesforce and have question regarding trigger. Please help me on this.

I want to write a trigger on below:
Create an account record whenever contact is created without an account.

Thanks in advance..!!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Adhya,

Please find the below piece of code 
 
trigger ContactTrigger1 on Contact (before insert) {

 List <Contact> createAccounts = new List<Contact>();  
   for (Contact con : trigger.new)   
   {  
     if (String.isBlank(con.accountid))   
     {  
       createAccounts.add(con);  
     }  
   }  
   if (createAccounts.size() > 0)   
   {  
    List <Account> newAccounts = new List<Account>();  
    Map<String,Contact> contactsByNameKeys = new Map<String,Contact> ();  
    
     for (Contact con : createAccounts)   
     {  
       String accountName = con.firstname + ' ' + con.lastname;  
       contactsByNameKeys.put(accountName,con);  
       Account acc = new Account(Name = accountName);  
       newAccounts.add(acc);  
     }  
     Insert newAccounts;  
     for (Account acc : newAccounts)   
     {  
       //Put Account Id's on Contacts  
       if (contactsByNameKeys.containsKey(acc.Name))   
       {  
         contactsByNameKeys.get(acc.Name).accountId = acc.Id;  
       }  
     }  
   }   
 }

If this solution helps, Please mark it as best answer.

Thanks,
 
CharuDuttCharuDutt
Hii Adhya
Try Below Trigger
trigger TestTrigger on Contact (before insert) {
 for(Contact Con : trigger.new){
        if(Con.AccountId == null){
            Account Acc = new Account();
            Acc.Name = Con.LastName;
            insert Acc;
            con.AccountId = Acc.Id;
        }
     
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
ngfcj hgfjhgngfcj hgfjhg
I am working on a same code and you can see the details abouty trigger here (https://removalsoldham.co.uk/) and try to apply it on your project I hope it will work for you.
Suraj Tripathi 47Suraj Tripathi 47

Hi Adhya,

I write a code for you I hope it will you
 
trigger InsertCon on Contact (before insert) {
 if(trigger.isInsert && trigger.isBefore){
   contactHelper.CreateCon(trigger.New);
    }
}
public class contactHelper{
public static void CreateCon(list<contact> conList){
    Account accObj = [select id from Account where Name like 'Adhya' limit 1];
    
 for(Contact conObj:ConList){
  conObj.AccountID = accObj.Id;
     }
  }
}

If you find your Solution then mark this as the best answer.
  
Thank you!

Regards,
Suraj Tripathi  
James henry 18James henry 18
I'm dealing with an equivalent code and you can see the subtleties abouty trigger here (https://crackview.com/) and attempt to apply it on your task I trust it will work for you.
AdhyaAdhya
@suraj and others can you explain why did you use before context in trigger?
It's very confusing to me when should I use Before and After events.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Adhya,

Here are some reference links when to use Before Trigger and After Trigger.

Hope these will help you to understand more on when to go with Before Trigger and when to choose After Trigger.

http://​​​​​​​https://www.levelupsalesforce.com/difference-between-before-and-after-triggers

https://www.salesforcepoint.com/2017/02/when-to-use-before-triggers-after.html


Thanks,
​​​​​​​
John James 7John James 7
Hey Adhya, 

Here are some reference joins when to use Before Trigger and After Trigger. https://www.seatcushion.com.au/  (https://www.seatcushion.com.au/)

Expectation these will assist you with seeing more on when to go with Before Trigger and when to pick After Trigger. 

https://www.levelupsalesforce.com/difference-between-before-and-after-triggers

https://www.salesforcepoint.com/2017/02/when-to-use-before-triggers-after.html

Much appreciated,
Rajeevan SRajeevan S
Hi Adhya,

You can try this code,
 
trigger createAccountForContact on Contact (before insert) {
	List<Contact> conList = new List<Contact>();
    for(Contact con: trigger.new){
        if(String.isBlank(con.accountId)){
            conList.add(con);
        }
    }
    Map<String, Account> accConMap = new Map<String, Account>();
    for(Contact c: conList){
        Account acc = new Account(Name=c.FirstName+' '+c.LastName);
        accConMap.put(c.FirstName+' '+c.LastName, acc);
    }
    if(!accConMap.keySet().isEmpty()){
        insert accConMap.values();
    }
    for(Contact c: trigger.new){
        if(accConMap.containsKey(c.Name)){
            c.AccountId  = accConMap.get(c.Id).Id;
        }
    }
}

Mark this answer if this helps.
 
erwgd hbgfhgferwgd hbgfhgf
Every person faced trigger issue in the start I mean majority of people as I faced but you can check here (https://www.limoagency.co.uk/barry/) some detailed information abot trigger management.