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
sonali  vermasonali verma 

Update contact description field

Hi friends
I am using a child relationship query to update contact description fields whenever an account record is created or updated.

Apex class code & trigger code is workign fine but I am having an issue in test class. The code coverage is showing 72% .  

Apex class code;
public class Account_ContactDescription
{
    public void Update_Description(Set<Id> accountIds)
    {
                    
           list<Account> accountsWithContacts =[select Id,Name, (select Id,FirstName,LastName from Contacts) from Account 
                                                                           where Id IN : accountIds];
           
           list<Contact> contactsToUpdate=new list<Contact>();
           
           for(Account a:accountsWithContacts)   //loop through all queries 
          {
                 for(Contact convals:a.Contacts)
                {
                        convals.Description=convals.FirstName+' '+convals.LastName;
                         
                        contactsToUpdate.add(convals);
               }
           }
           
           update contactsToUpdate;
    }
}

Apex Trigger code:
trigger trg_UpdateContactDescription on Account (before Update)
{
    Account_ContactDescription c=new Account_ContactDescription();
     
    Map<Id,Account> amap = new Map<Id,Account>([select Id from Account]);
     
    Set<Id> accountIds = amap.KeySet();
     
    c.Update_Description(accountIds);
}          


Test class code:
@istest(SeeAllData=false)
private class AccountContactDescription_Test
{
    private static testmethod void validatetest()
    {
         Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
         User u = new User(Alias = 'sample', Email='standarduser@testorg.com', 
         EmailEncodingKey='UTF-8', LastName='sampleLname', LanguageLocaleKey='en_US', 
         LocaleSidKey='en_US', ProfileId = p.Id, 
         TimeZoneSidKey='America/Los_Angeles', UserName='testnamesss@testorg.com');
         system.runAs(u)
         {
              Account  acn = new Account();
              acn.Name='test1';
                            
              Database.Insert(acn);
                       
                                        
              Contact con = new Contact();
              
              con.LastName='test3';
                                          
              con.FirstName='test4'; 
              
              Database.Insert(con); 
              
              acn.Name='test2';
              Database.Update(acn);     
        
         }
       }
   }

How to update the  description field but not sure how to go about it.
Can anyone pls le tme know:?

regards
sonali verma
Best Answer chosen by sonali verma
Arunkumar RArunkumar R
Hi

Check out the below modified class and test class. This will give 100% code coverage, In your test class while inserting contact record you missed to add account to that contact. This is the reason you have not get coverage.

Trigger:
trigger trg_UpdateContactDescription on Account (before Update)
{
    if(Trigger.isUpdate && Trigger.isBefore)
    {
        Account_ContactDescription.Update_Description(Trigger.newMap.keySet());
    }
}

Apex Class:
public class Account_ContactDescription
{
    public static void Update_Description(Set<Id> accountIds)
    {
                    
           list<Account> accountsWithContacts =[select Id,Name, (select Id,FirstName,LastName from Contacts) from Account 
                                                                           where Id IN : accountIds];
           
           list<Contact> contactsToUpdate=new list<Contact>();
           
           for(Account a:accountsWithContacts)   //loop through all queries 
          {
                 for(Contact convals:a.Contacts)
                {
                        convals.Description=convals.FirstName+' '+convals.LastName;
                        contactsToUpdate.add(convals);
               }
           }
           
           update contactsToUpdate;
    }
}

Test Class:
 
@istest(SeeAllData=false)
private class AccountContactDescription_Test
{
    private static testmethod void validatetest()
    {
         Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
         User u = new User(Alias = 'sample', Email='standarduser@testorg.com', 
         EmailEncodingKey='UTF-8', LastName='sampleLname', LanguageLocaleKey='en_US', 
         LocaleSidKey='en_US', ProfileId = p.Id, 
         TimeZoneSidKey='America/Los_Angeles', UserName='testnamesss@testorg.com');
         system.runAs(u)
         {
              Account  acn = new Account();
              acn.Name='test1';
              insert acn;
              
              Contact con = new Contact();
              con.LastName='test3';
              con.FirstName='test4'; 
              con.AccountId = acn.Id;
              insert con; 
              
              acn.Name='test2';
              update acn;     
        
         }
      }

}

All Answers

Arunkumar RArunkumar R
Hi

Check out the below modified class and test class. This will give 100% code coverage, In your test class while inserting contact record you missed to add account to that contact. This is the reason you have not get coverage.

Trigger:
trigger trg_UpdateContactDescription on Account (before Update)
{
    if(Trigger.isUpdate && Trigger.isBefore)
    {
        Account_ContactDescription.Update_Description(Trigger.newMap.keySet());
    }
}

Apex Class:
public class Account_ContactDescription
{
    public static void Update_Description(Set<Id> accountIds)
    {
                    
           list<Account> accountsWithContacts =[select Id,Name, (select Id,FirstName,LastName from Contacts) from Account 
                                                                           where Id IN : accountIds];
           
           list<Contact> contactsToUpdate=new list<Contact>();
           
           for(Account a:accountsWithContacts)   //loop through all queries 
          {
                 for(Contact convals:a.Contacts)
                {
                        convals.Description=convals.FirstName+' '+convals.LastName;
                        contactsToUpdate.add(convals);
               }
           }
           
           update contactsToUpdate;
    }
}

Test Class:
 
@istest(SeeAllData=false)
private class AccountContactDescription_Test
{
    private static testmethod void validatetest()
    {
         Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
         User u = new User(Alias = 'sample', Email='standarduser@testorg.com', 
         EmailEncodingKey='UTF-8', LastName='sampleLname', LanguageLocaleKey='en_US', 
         LocaleSidKey='en_US', ProfileId = p.Id, 
         TimeZoneSidKey='America/Los_Angeles', UserName='testnamesss@testorg.com');
         system.runAs(u)
         {
              Account  acn = new Account();
              acn.Name='test1';
              insert acn;
              
              Contact con = new Contact();
              con.LastName='test3';
              con.FirstName='test4'; 
              con.AccountId = acn.Id;
              insert con; 
              
              acn.Name='test2';
              update acn;     
        
         }
      }

}
This was selected as the best answer
Srinath TerampattilSrinath Terampattil
I would suggest you to use the Process Builder to achieve this. Currently you can update the Description field from Account to the child Contacts, whenever account is created or updated using Process builder.

Thanks!
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Sonali,
There is a simple mistake in your test class. you have not put the Account Id into your Contact. The only thing you need to change is ,
 
system.runAs(u)
{
	Account  acn = new Account();
	acn.Name='test1';
	Database.Insert(acn);
		   
							
	Contact con = new Contact();
	con.AccountId = acn.Id // My change in code
	con.LastName='test3';
	con.FirstName='test4'; 
	Database.Insert(con); 

	acn.Name='test2';
	Database.Update(acn);     

}

Thanks :) 
sonali  vermasonali verma
Hi Prosenjit

Thanks, I just releaised my mistake.

Thanks
sonali