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
bhanu challengebhanu challenge 

how to write TEST class for this trigger.plz check and help testclass code

trigger ContactPhone on Account (Before update) 
{
List<Contact> listConForPhoneUpdate;

    for (Account acc : Trigger.New)
        {
        listConForPhoneUpdate = [Select Phone from Contact   where phone!=null and  AccountID =: acc.id];
        for(Contact con : listConForPhoneUpdate )
            con.Phone = acc.Phone;
        }
update listConForPhoneUpdate;
}
Best Answer chosen by bhanu challenge
Nayana KNayana K
Please follow best practices. SOQL inside for loop is not recommended.
https://developer.salesforce.com/blogs/developer-relations/2015/01/apex-best-practices-15-apex-commandments.html 

You can optimize your trigger like this :
trigger ContactPhone on Account (After update) 
{
	List<Contact> lstConForPhoneUpdate = new List<Contact>();
	Map<Id, String> mapAccIdToPhone = new Map<Id, String>();
	
	for (Account objAcc : Trigger.New)
	{
		// if phone is changed
		if(objAcc.Phone != Trigger.oldMap.get(objAcc.Id).Phone)
			mapAccIdToPhone.put(objAcc.Id, objAcc.Phone);
	}
	
	// iterate over contacts of the accounts whose phone is updated
	for(Contact objContact : [	SELECT Phone, AccountId 
								FROM Contact   
								WHERE AccountId IN: mapAccIdToPhone.keySet()
								])
	{
		// assign phone
		objContact.Phone = mapAccIdToPhone.get(objContact.AccountId);
		lstConForPhoneUpdate.add(objContact);
	}
	
	if(!lstConForPhoneUpdate.isEmpty())
		update lstConForPhoneUpdate;
}
 
@isTest
public class ContactPhoneTest
{
	static testMethod void testForContactPhoneUpdate() 
	{
		List<Contact> lstContact = new List<Contact>();
		
// Don't forget to include required field value pairs in Account and Contact which I am inserting here.
		Account objAccount = new Account(Name = 'Test Acc', Phone = '12345678');
		insert objAccount;
		
		for(Integer i=0; i< 10; i++)
		{
			lstContact.add(new Contact(LastName = 'Test Con'+i, AccountId = objAccount.Id));
		}
		insert lstContact;
		
		// update account's phone 
		objAccount.Phone = '12345679';
		update objAccount;
		
		for(Contact objCon : [SELECT Phone FROM Contact WHERE AccountId =: objAccount.Id])
		{
			system.assertEquals(objAccount.Phone, objCon.Phone);
		}
	}
}

 

All Answers

Nayana KNayana K
Please follow best practices. SOQL inside for loop is not recommended.
https://developer.salesforce.com/blogs/developer-relations/2015/01/apex-best-practices-15-apex-commandments.html 

You can optimize your trigger like this :
trigger ContactPhone on Account (After update) 
{
	List<Contact> lstConForPhoneUpdate = new List<Contact>();
	Map<Id, String> mapAccIdToPhone = new Map<Id, String>();
	
	for (Account objAcc : Trigger.New)
	{
		// if phone is changed
		if(objAcc.Phone != Trigger.oldMap.get(objAcc.Id).Phone)
			mapAccIdToPhone.put(objAcc.Id, objAcc.Phone);
	}
	
	// iterate over contacts of the accounts whose phone is updated
	for(Contact objContact : [	SELECT Phone, AccountId 
								FROM Contact   
								WHERE AccountId IN: mapAccIdToPhone.keySet()
								])
	{
		// assign phone
		objContact.Phone = mapAccIdToPhone.get(objContact.AccountId);
		lstConForPhoneUpdate.add(objContact);
	}
	
	if(!lstConForPhoneUpdate.isEmpty())
		update lstConForPhoneUpdate;
}
 
@isTest
public class ContactPhoneTest
{
	static testMethod void testForContactPhoneUpdate() 
	{
		List<Contact> lstContact = new List<Contact>();
		
// Don't forget to include required field value pairs in Account and Contact which I am inserting here.
		Account objAccount = new Account(Name = 'Test Acc', Phone = '12345678');
		insert objAccount;
		
		for(Integer i=0; i< 10; i++)
		{
			lstContact.add(new Contact(LastName = 'Test Con'+i, AccountId = objAccount.Id));
		}
		insert lstContact;
		
		// update account's phone 
		objAccount.Phone = '12345679';
		update objAccount;
		
		for(Contact objCon : [SELECT Phone FROM Contact WHERE AccountId =: objAccount.Id])
		{
			system.assertEquals(objAccount.Phone, objCon.Phone);
		}
	}
}

 
This was selected as the best answer
bhanu challengebhanu challenge
thankq................