I am having a hard time to run my test class for my trigger WHERE my trigger 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 accountId = new Set();
List contactsToUpdate = new List();

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


update contactsToUpdate;


It works fine.But my test class is giving me errors: Compile Error: Initial term of field expression must be a concrete SObject: List at line 24 column 41

My Test Class

public class UpdateContactBillingAddress_Test{
static testMethod void testupdates() {

// Let’s create our records from scratch!
Account a= new Account();
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();
List contact = [SELECT Id, MailingStreet,MailingCity FROM Contact WHERE AccountId = :a.Id];
System.assertEquals(‘hjasadhj’, contact.MailingCity);


Can anyone help me find the error/debug it. Thanks


it is jus this line :

List contact = [SELECT Id, MailingStreet,MailingCity FROM Contact WHERE AccountId = :a.Id];

make it

List< Contact > cnt = [SELECT Id, MailingStreet,MailingCity FROM Contact WHERE AccountId = :a.Id];

it should work

Also in assert statement use contact[0].MailingCity instead of just contact.MailingCity

| improve this answer | |
  • Its still giving Errors. – Bookeraward2 Mar 3 '15 at 22:31
  • Error:Class name is already in use or has been previously used – Bookeraward2 Mar 3 '15 at 22:32
  • Thats a different error. so your previous problem is solved. go to setup>> develop>> class , find your class , edit and save the code again. – Manjunath Nimbal Mar 3 '15 at 22:35
  • Now the code is saved,but when I run it ,it fails Error Message:System.ListException: List index out of bounds: 0 Stack Trace:Class.UpdateContactBillingAddress.testupdates: line 23, column – Bookeraward2 Mar 3 '15 at 22:52
  • Thats beacuse you have not inserted contact in the test class. just before you query just do : insert c – Manjunath Nimbal Mar 4 '15 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.