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
Salesforce5566Salesforce5566 

how to write test classes for below trigger ?

rigger SameEmailOnAllContacts on Account (after update) {
    Set<Id> setAccountId = new Set<Id>();
    for(Account acc:trigger.new) 
    {
        if(Trigger.oldMap.get(acc.ID).Email__c != acc.Email__c || Trigger.oldMap.get(acc.ID).phone != acc.phone) {
            setAccountId.add(acc.id);
        }
    }
    List<Contact> lstContact = new List<Contact>();
    
    for(Account acc:[Select id,Email__c,(select id,Email from Contacts) from Account where Id in:setAccountId])
    {
        for(Contact con:acc.contacts)
        {  
            if(con.Email != acc.Email__c) 
            {
                con.Email = acc.Email__c;
                lstContact.add(con);
            }
        }
    }
    if(lstContact.size() >0) {
        update lstContact;
    }
}

Please help me on this !!!!

 
@isTest
public class SameEmailOnAllContactsTest{
 public static testMethod void Test()
 {
 
 Account acct=new Account();
        acct.name='a';
        acct.industry='Electronics';   
        insert acct;
        
         Contact ct = new Contact();
         
         ct.LastName ='Hoal';
         
          insert ct;
 
 }
 }

 
Best Answer chosen by Salesforce5566
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
@isTest
public class SameEmailOnAllContactsTest {
    
    public static testMethod void Test() {
        
        Account acct = new Account();
        acct.Name = 'Test Acc';
        acct.Phone = '1234';
        acct.Email__c = 'acct1@test.com';
        INSERT acct;
        
        Contact ct = new Contact();
        ct.AccountId = acct.Id;
        ct.LastName = 'Test Con';
        ct.Email = 'ct@test.com';
        INSERT ct;
        
        Test.StartTest(); 
        acct.Name ='Test Acc 1';
        acct.Email__c = 'acct2@test.com';
        UPDATE acct;
        Test.StopTest();
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
@isTest
public class SameEmailOnAllContactsTest {
    
    public static testMethod void Test() {
        
        Account acct = new Account();
        acct.Name = 'Test Acc';
        acct.Phone = '1234';
        acct.Email__c = 'acct1@test.com';
        INSERT acct;
        
        Contact ct = new Contact();
        ct.AccountId = acct.Id;
        ct.LastName = 'Test Con';
        ct.Email = 'ct@test.com';
        INSERT ct;
        
        Test.StartTest(); 
        acct.Name ='Test Acc 1';
        acct.Email__c = 'acct2@test.com';
        UPDATE acct;
        Test.StopTest();
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Salesforce5566Salesforce5566
Thanks Khan!!!!