You need to sign in to do that
Don't have an account?

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.
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.
All Answers
However, in your test class you you are testing mailing Street
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
This uses the map functionality to check to see if it's really been updated.
Hope this helps.
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.
@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!!
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);
}
}
Thanks Bogdan.
I really appreciate your help.
So when you are searching for a matching ID in your WHERE statement, it's not getting a match.