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
Feel Free To AskFeel Free To Ask 

Email Field auto update when created a new record with account id != null.

I have created a custom email field on account object.
I want that when ever i create a new contact which uses account object in account field should use the account email field to fill in contact email field.

I am a newbie and just wanna practice. 

Thanks in advance fir all your suggestions. 
mukesh guptamukesh gupta
Hi Suvimal,

Please use below code:-

Account_Email_c need to replace with your field 
Trigger ContactCreation on COntact(After Insert){

Set<Id> contIds = new Set<Id>();
List<Contact> updateCOntact = new List<Contact>();
 for(Contact con : Trigger.New()){
     contIds.add(con.AccountId);
 
 }
 
 List<Account> accList = [Select Id, Account_Email_c (Select Id, Email From Contacts)from Account where Id =: contIds];
 
 
 
 for(Account acc: accList){
 
 for(Contact con : acc.Contacts){
 
   con.Email = acc.Account_Email_c;
   
   updateCOntact.add(con);
 }
 
 
 }
 
 if(updateCOntact.size() >0){
 
   update updateCOntact;
 }
}

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

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

Thanks
Mukesh