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
bookerawardbookeraward 

Test Class for Updating a Trigger

Hello Everyone,

I am trying to write a test class for my trigger which updates the Contacts Mailing to Accounts Biliing Address whenever the  Accounts Billing Address is updated.
This is my Trigger

trigger UpdateContactBillingAddress on Account (after update) {

    Set<Id> accountId = new Set<Id>();
    List<Contact> contactsToUpdate = new List<Contact>();
    
    for(Account acc: Trigger.new){

        // Please perform a check to see if address was updated

        accountId.add(acc.Id); //accounts that were updated.
    }
    
    for(Contact c : [Select Id, MailingCity, MailingStreet, MailingCountry, 
                            Account.BillingCity, Account.BillingStreet, Account.BillingCountry 
                      from Contact where Account.Id in: accountId]){

                 c.MailingCity = c.Account.BillingCity;
                 ///same for rest of the fields
                 
                 contactsToUpdate.add(c);
    }

    update contactsToUpdate;
    
}

I am having a hard time writing its test Class,This is what I have done so far:
@isTest 
public class UpdateContactBillingAddress_Test{
    static testMethod void testupdates() {

     // Let's create our records from scratch!
       Account a= new Account();
       a.Name='TestAccount';
       a.BillingStreet='hjasadhj';
       insert a;
        Contact c = [SELECT Id, MailingStreet,MailingCity FROM Contact WHERE AccountId = :a.Id];
        System.assertEquals('hjasadhj', c.MailingStreet);
        System.assertEquals('High', c.MailingStreet);
       }
      }

Why am i getting error while running the code?
Any help will be appreciated.
Thanks.
Best Answer chosen by bookeraward
ShotShot
you added just a code, not an insert operation:
c.AccountId = a.Id;
insert c;

 

All Answers

Mathew Andresen 5Mathew Andresen 5
One thing I noticed right off the bat, is in your trigger you have  mailing City being updated
 
c.MailingCity = c.Account.BillingCity;

However, in your test class you you are testing mailing Street
 
System.assertEquals('hjasadhj', c.MailingStreet);
        System.assertEquals('High', c.MailingStreet);

In addition, you've only inserted the account here. The trigger fires after update.  So after inserting the account, you should update the account with a different address.  Also, you need to create the contact for the account as well.  Remember all data needs to be created for a test class




also,  the code to test to see if the address has actually been updated would be
 
// Please perform a check to see if address was updated
        Account oldAcct = Trigger.OldMap.get(acc.Id);
        If (oldAcct.BillingAddress != acc.BillingAddress) {
           accountId.add(acc.Id); //accounts that were updated. 
        }

This uses the map functionality to check to see if it's really been updated.

Hope this helps.  
bookerawardbookeraward
Thanks Mattew,
I have made changes to my code

@isTest 
public class UpdateContactBillingAddress_Test{
    static testMethod void testupdates() {

     // Let's create our records from scratch!
       Account a= new Account();
       a.Name='TestAccount';
       a.BillingStreet='hjasadhj';
       insert a;
       
   
  // Please perform a check to see if address was updated
        Account olda = Trigger.OldMap.get(a.Id);
        If (olda.BillingAddress != a.BillingAddress) {
           accountId.add(acc.Id); //accounts that were updated. 
        }
   //Create record for new contact
       Contact c=new Contact();
       c.FirstName='hina';
       c.LastName='meena';
        List<Contact> contact = [SELECT Id, MailingStreet,MailingCity FROM Contact WHERE AccountId = :a.Id];
        System.assertEquals('hjasadhj', contact.MailingCity);
       
        
       }
      }

I am not sure how to update my ACCOUNT address.I tried using Update but its not working??
And it still gives errors
I am new to the Salesforce,having hard time with my Test Class.

 
ShotShot
insert operation dont have Trigger.old and also you didnt connect contact with account and didnt insert this contact
bookerawardbookeraward
Here is what is got so far:

@isTest
public class UpdateContactBillingAddress{
static testMethod void testupdates() {

// Let’s create our records from scratch!
 Account a= new Account();
 a.Name='TestAccount';
 a.BillingStreet='hjasadhj';
 insert a;
//update Account
 a.billingState='My City';
 update a;

// Verify that the billingState field was updated in the database.
 Account updatedAccount = [SELECT billingState FROM Account WHERE Id = :a.Id];
 System.assertEquals('My City', updatedAccount.billingState);

//Create record for new contact
 //Contact c=new Contact();
 //c.FirstName='hina';
 //c.LastName='meena';
 List <Contact> c = [SELECT Id, MailingStreet,MailingCity FROM Contact WHERE AccountId = :a.Id];
 System.assertEquals('My City', c[0].MailingCity);

  }
}

But its giving me Error!!
ShotShot
of course, you dont have any contacts
bookerawardbookeraward
Added contacts,the code still gives me
Error:Error Message System.ListException: List index out of bounds: 0
Stack Trace Class.UpdateContactBillingAddress.testupdates: line 23, column 1


Code:
@isTest
public class UpdateContactBillingAddress{
static testMethod void testupdates() {

// Let’s create our records from scratch!
 Account a= new Account();
 a.Name='TestAccount';
 a.BillingStreet='hjasadhj';
 insert a;
//update Account
 a.billingState='My City';
 update a;

// Verify that the billingState field was updated in the database.
 Account updatedAccount = [SELECT billingState FROM Account WHERE Id = :a.Id];
 System.assertEquals('My City', updatedAccount.billingState);

//Create record for new contact
 Contact c=new Contact();
 c.FirstName='hina';
 c.LastName='meena';
 List <Contact> contact = [SELECT Id, MailingStreet,MailingCity FROM Contact WHERE AccountId = :a.Id];
 System.assertEquals('My City', contact[0].MailingCity);

  }
}

 
ShotShot
you added just a code, not an insert operation:
c.AccountId = a.Id;
insert c;

 
This was selected as the best answer
bookerawardbookeraward
Got it.
Thanks Bogdan.
I really appreciate your help.
Mathew Andresen 5Mathew Andresen 5
You haven't inserted the contact, or attached the contact to the account
Contact c=new Contact();
 c.FirstName='hina';
 c.LastName='meena';
c.accountId = a.id;

insert(c);

So when you are searching for a matching ID in your WHERE statement, it's not getting a match.