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
Ganesh Patil 78Ganesh Patil 78 

Create contact through triggers when account name got change with default data on contacts.

Best Answer chosen by Ganesh Patil 78
CharuDuttCharuDutt
Hii Ganesh Patil
Try Below trigger
trigger accountcontat on Account (before update){
    list<Contact> lstCon = new list<Contact>();
    for(Account Acc :trigger.new){
        if(Acc.Name != Trigger.oldMap.get(Acc.Id).Name){
        Contact Con = new Contact();
        Con.FirstName = Acc.Name;
        Con.LastName ='Contact';
        Con.AccountId = Acc.Id;    
        lstCon.add(Con);
        }
    }
    if(lstCon.size()>0){
        Insert lstCon;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 

All Answers

CharuDuttCharuDutt
Hii Ganesh Patil
Try Below trigger
trigger accountcontat on Account (before update){
    list<Contact> lstCon = new list<Contact>();
    for(Account Acc :trigger.new){
        if(Acc.Name != Trigger.oldMap.get(Acc.Id).Name){
        Contact Con = new Contact();
        Con.FirstName = Acc.Name;
        Con.LastName ='Contact';
        Con.AccountId = Acc.Id;    
        lstCon.add(Con);
        }
    }
    if(lstCon.size()>0){
        Insert lstCon;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
This was selected as the best answer
mukesh guptamukesh gupta
Hi Ganesh,

Please use below code:-
 
trigger CreateContact on Account (after update){

    if(Trigger.isupdate){
    
        List<Contact> ct = new List <Contact>();
        
        for(Account acc : trigger.new){
		 if(Acc.Name != Trigger.oldMap.get(Acc.Id).Name){
        Contact c = new Contact(LastName = acc.name,
                        AccountId=acc.id,
                        Fax=acc.Fax,
                        MailingStreet=acc.BillingStreet,
                        MailingCity=acc.BillingCity,
                        MailingState=acc.BillingState,
                        MailingPostalCode=acc.BillingPostalCode,
                        MailingCountry=acc.BillingCountry,
                        Phone=acc.Phone);

        ct.add(c);
        
        }
        
        if(!ct.isEmpty())
            insert ct; 
    }
	}
	}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 


 
ravi soniravi soni
Hy Ganesh,
find your solution by using below trigger.
trigger CreateContat on Account (After update){
	if(trigger.isAfter && trigger.isUpdate){
    list<Contact> lstCon = new list<Contact>();
    for(Account Acc :trigger.new){
        if(Acc.Name != Trigger.oldMap.get(Acc.Id).Name){
        Contact Con = new Contact();
        Con.FirstName = Acc.Name;
        Con.LastName ='Contact';
        Con.AccountId = Acc.Id;    
        lstCon.add(Con);
        }
    }
    if(lstCon.size()>0){
        Insert lstCon;
    }
	}
}

let me know if it helps you and don't forget to mark it as best answer.
Thank you
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please find the solution.

trigger CreateContact on Account (before update){
    List<Contact> contactList = new List<Contact>();
    for(Account ac :trigger.new){
        if(ac.Name != Trigger.oldMap.get(ac.Id).Name){
        Contact Con = new Contact();
        Con.FirstName = 'Default';
        Con.LastName ='Contact';
        Con.AccountId = ac.Id;    
        contactList.add(Con);
        }
    }
    if(contactList.size()>0){
        Insert contactList;
    }
}

Thank you