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
sam patilsam patil 

Create an email field on Account. Write a trigger such that if the Email field is not empty and is changed then all the child contact records must have the parent Account Email field value populated. Write test class as well.

Best Answer chosen by sam patil
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sam,

Can you try the below apex trigger and test class.

Trigger:
trigger CopyAccountEmailToContactEmail on Account (after update) {
    Map<Id, String> m = new Map<Id, String>();
    for (Account a : Trigger.new) {
        Account old = Trigger.oldMap.get(a.Id);
        
        if (a.Email__c != old.Email__c) {
            m.put(a.Id, a.Email__c);
        }
    }
    if (m.size() > 0) {
        Contact[] contacts = [
                select Id, AccountId
                from Contact
                where AccountId in :m.keySet()
                ];
        for (Contact c : contacts) {
            c.Email = m.get(c.AccountId);
        }
        update contacts;
    }
}

Test Class:
@istest
public class EmailCopyTest {
    static testMethod void myTest() {
        
        Account acc= new Account();
        acc.name='sample';
        acc.Email__c='sample@gmail.com';
        insert acc;
        Contact con= new Contact();
        con.lastname='sample';
        con.AccountId=acc.id;
        insert con;
		acc.Email__c='sample123@gmail.com';
		update acc;
		system.assertEquals('sample123@gmail.com', acc.Email__c) ;       
    }

}

Let me know if you face any issues.

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

Thanks
​​​​​​​

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sam,

Can you try the below apex trigger and test class.

Trigger:
trigger CopyAccountEmailToContactEmail on Account (after update) {
    Map<Id, String> m = new Map<Id, String>();
    for (Account a : Trigger.new) {
        Account old = Trigger.oldMap.get(a.Id);
        
        if (a.Email__c != old.Email__c) {
            m.put(a.Id, a.Email__c);
        }
    }
    if (m.size() > 0) {
        Contact[] contacts = [
                select Id, AccountId
                from Contact
                where AccountId in :m.keySet()
                ];
        for (Contact c : contacts) {
            c.Email = m.get(c.AccountId);
        }
        update contacts;
    }
}

Test Class:
@istest
public class EmailCopyTest {
    static testMethod void myTest() {
        
        Account acc= new Account();
        acc.name='sample';
        acc.Email__c='sample@gmail.com';
        insert acc;
        Contact con= new Contact();
        con.lastname='sample';
        con.AccountId=acc.id;
        insert con;
		acc.Email__c='sample123@gmail.com';
		update acc;
		system.assertEquals('sample123@gmail.com', acc.Email__c) ;       
    }

}

Let me know if you face any issues.

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

Thanks
​​​​​​​
This was selected as the best answer
sam patilsam patil
thanks 
Sai Praveen  for your respond.!!