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
Rakhi B 10Rakhi B 10 

create a contact if account id null create contact lastname ,email using create account that account like to the contact object.

SandhyaSandhya (Salesforce Developers) 
Hi,

Below is the sample which you can modify to your needs.
 
Trigger on Account object to handle insert and update.
trigger insertcontact on Account (before insert,after insert,after update) {
    if(trigger.isbefore && trigger.isinsert) {
		ContactNameUpdate.accountmethod(Trigger.new);
    }
    
    if(trigger.isafter && trigger.isinsert) {
		ContactNameUpdate.contactMethod(Trigger.new);
    }
}
 
public class ContactNameUpdate {
	// Method to handle the contact inserts.
	public static void contactMethod(List<Account> accList){
		List<Contact> conList = new List<Contact>();
		for(Account acc : accList) {
			  Contact con = new Contact(AccountId = acc.Id);
			  List<String> nameStr = acc.Name.split(' ');
			  if(nameStr.size()>0)
				 con.LastName = nameStr[0];
			  if(nameStr.size()>1)
				  con.FirstName = nameStr[1];
			  conList.add(con);
		}
		insert conList;		
	}
}

Please mark it as solved if my reply was helpful, it will make it available
for others as a proper solution.

Best Regards,
​Sandhya