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
AzusfdcAzusfdc 

I want when account is created automatically contact should creates using with invoking trigger to class below is code i tried any suggestions?

public class contactnameupdate
{
    public static void accountmethod(list<account> acclist)
    {
    string s='http://tekslate.com/15-sample-triggers-different-scenarios/';
        for(account a:acclist)
        {
            a.description=s;
        }
    }
    
    public static void contactmethod(list<contact> conlist)
    {
    list<account> accconlist=new list<account>();
    for(contact c:conlist)
    {
    accconlist.add(new account(id=c.accountid,name=c.lastname));
    }
    }

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

 
Naval Sharma4Naval Sharma4
Hi Azu,

I've modified your code and now it should work.
 
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 static void contactmethod(List<Account> accountList){
    List<Contact> contactList = new List<Contact>();
    for(Account acc : accountList)
    {
          Contact con = new Contact(AccountId = acc.Id);
          List<String> Name = acc.Name.split(' );
          if(Name.size()>1)
             con.LastName = Name[1];
          if(Name.size()>0)
              con.FirstName = Name[0];
          contactList.add(con);
      }
      insert contactList;
    }

 
Mahesh DMahesh D
Hi Azu,

Please find the below Trigger and Class:
 
// 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()>1)
				 con.LastName = nameStr[1];
			  if(nameStr.size()>0)
				  con.FirstName = nameStr[0];
			  conList.add(con);
		}
		insert contactList;		
	}
}

Modified the code to follow best practices and minor syntax issue.

Please do let me know if it helps you.

Regards,
Mahesh
Mahesh DMahesh D
Hi Azu,

Please take the latest code which I tested in my DE environment and working properly with all scenarios.
 
// 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 do let me know if it helps you.

Regards,
Mahesh
Payal Bhoyar 10Payal Bhoyar 10
Hi Azu,
Try this
Trigger :
trigger AccountTrigger on Account (after insert, after update) 
{
   Trigger.isInsert && Trigger.isAfter)
    {
        ContactHandler.insertContact(Trigger.new);
    }
    
    if(Trigger.isUpdate && Trigger.isAfter)
    {
        ContactHandler.updateContact(Trigger.new);
    }
}

Class Handler : 

public class ContactHandler{
public static void insertContact(List<Account> accList)
    {     
           try
        {
            System.debug('Insert Contact Method');
            Map <String, Account> contatctNameVSAccount = new Map<String, Account>(); 
            List<Contact> contList = new List<Contact>();
            
            for(Account acc : accList)
            {
                Contact contObj = new Contact();
                contObj.Lastname = acc.Name;
                contObj.AccountId = acc.Id;
                contatctNameVSAccount.put(contObj.LastName, acc);
                contList.add(contObj);
            }
            insert contList;
            System.debug('contList ' +contList);
        }
        catch(Exception e)
        {
            System.debug('Exception ' +e);
            System.debug('Exception line ' +e.getLineNumber());
            System.debug('Exception message ' +e.getMessage());
            System.debug('Exception stack trace string ' +e.getStackTraceString());
            System.debug('Exception type name ' +e.getTypeName());
        }*/
    }
    
    /*public static void updateContact(List<Account> accList)
    {
        try
        {
            List<Id> accId = new List<Id>();
            for(Account acc : accList)
            {
                accId.add(acc.Id);            
            }
             List<Contact> contList = [SELECT Id,Name, Update_of_Contact__c FROM Contact WHERE AccountId in: accId];
            for(Contact ac : contList)
            {
                //Update_of_Contact__c  coustom field to check how many time contact update
                if(ac.Update_of_Contact__c == null)
                {
                    ac.Update_of_Contact__c = 1;
                }
                else
                    ac.Update_of_Contact__c = ac.Update_of_Contact__c+1;
            }
            update contList;
            System.debug('contList '+contList);
        }
        catch(Exception e)
        {
            System.debug('Exception ' +e);
            System.debug('Exception line ' +e.getLineNumber());
            System.debug('Exception message ' +e.getMessage());
            System.debug('Exception stack trace string ' +e.getStackTraceString());
            System.debug('Exception type name ' +e.getTypeName());
        }
    }
}*/
}

Please do let me known if it helps you
Payal
Suraj Tripathi 47Suraj Tripathi 47
Hi Azusfdc,
"Try this code."
 ---Trigger 
trigger insertcontactRecord on Account (before insert,after insert,after update) {
    if(trigger.isBefore){
       if(trigger.isInsert) {
        UpdateContact.accCreate(Trigger.new);
           }
    }
    
    else if(trigger.isAfter){
       if(trigger.isInsert) {
        UpdateContact.conCreate(Trigger.new);
    }
}
---apex class
public class UpdateContact{
    public static void conCreate(List<Account> accList){
        List<Contact> conList = new List<Contact>();
        for(Account acc : accList) {
              Contact con = new Contact();
                          con.AccountId = acc.Id;
              List<String> nameString = acc.Name.split(' ');
              if(nameString.size()>1)
                 con.LastName = nameString[1];
              if(nameString.size()>0)
                  con.FirstName = nameString[0];
              conList.add(con);
        }
        insert conList;        
    }
}
If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi