0

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

contactsToUpdate.add(c);
}

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

@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;
//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 = [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

1 Answer 1

0

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

6
  • Its still giving Errors. Mar 3, 2015 at 22:31
  • Error:Class name is already in use or has been previously used Mar 3, 2015 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.
    – Manju
    Mar 3, 2015 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 Mar 3, 2015 at 22:52
  • Thats beacuse you have not inserted contact in the test class. just before you query just do : insert c
    – Manju
    Mar 4, 2015 at 0:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .