2
votes

I came back to this 3 hours later and with no changes to any code, test execution worked. Disregard This Question

I have written a trigger that works great for setting the accountID on a contact record based on an external ID.

I had a test that worked great, but I added some logic to the trigger so it only updates contacts with a 'Default Account' as account. Now my test fails saying it did not update the account even though it still works as designed when saving records.

What am I missing?

I added line 4

ID DefaultAccID = [select ID from Account where Name = 'Default Account'].ID;

and the if around line 9

if (contactNew.AccountID == DefaultAccID) {
}

to Trigger:

trigger TRG_Contact_SetHouseholdID on Contact (before update, before insert) {
Set<String> AccIDlist = new Set<String>();
ID DefaultAccID = [select ID from Account where Name = 'Default Account'].ID;

// loop through records finding thoes that need to be linked
for(Contact contactNew:Trigger.New){
    if (contactNew.AccountID == DefaultAccID) {
        AccIDlist.add(contactNew.RPHouseholdID__c);
    }
}

Map<String, ID> AccountMap = new Map<String, ID>();

//loop needing linked and get account ID if it exist     
for(Account oneAccount:[SELECT RPHouseholdID__c, ID from Account where RPHouseholdID__c IN :AccIDlist]){
    AccountMap.put(oneAccount.RPHouseholdID__c, oneAccount.ID);
}

// loop through records updating the ones that need link
for(Contact contactNew:Trigger.New){
    if (AccountMap.get(contactNew.RPHouseholdID__c) <> null){
        contactNew.AccountID = AccountMap.get(contactNew.RPHouseholdID__c);
    }
}
}

Test Class:

@isTest
Private class TRG_Contact_SetHouseholdID_Test {
static TestMethod void Test0_TestInsertWithValue(){
    insertTestAccounts();
    Account defaultAccount = [select ID from Account where name = 'Default Account' limit 1];
    Account newAccount = [select ID from Account where name = 'Camper Family' limit 1];
    test.startTest();
        insertTestContact(defaultAccount.ID);
    test.stopTest();
    Contact newContact = [select ID,AccountID from Contact where firstname = 'Happy' and lastname = 'Camper' limit 1];
    System.assertEquals(newContact.AccountID, newAccount.ID, 'accountID did not get changed from default (' + defaultAccount.ID + ')');
}

private static void insertTestAccounts()
{
    Account obj = new Account();
    obj.Name = 'Default Account';
    insert obj;
    obj = new Account() ;
    obj.Name = 'Camper Family';
    obj.RPHouseholdID__c = '111';
    insert obj;

}
private static void insertTestContact(ID defaultID)
{
    Contact obj = new Contact();
    obj.AccountID = defaultID;
    obj.firstName = 'Happy';
    obj.lastname = 'Camper';
    obj.RPHouseholdID__c = '111';
    insert obj;
}
}
2

2 Answers

4
votes

Expanding a bit on @zachelrath 's answer, beginning with API version 24.0, test methods are isolated from the data in the organization (with a few exceptions User, RecordType, etc. see here for the full list). Rather than changing the API version of the file, the easiest thing to do is to add the see all data directive to the isTest annotation, like so:

@isTest(SeeAllData=true)
Private class TRG_Contact_SetHouseholdID_Test {
// etc
2
votes

Set your test class' API version to 24.0 --- I'd bet your initial query for the Default Account is pulling in a preexisting record whose Name is 'Default Account', instead of the Default Account you're creating in your test data. There are 2 ways to get around this:

  1. (Easiest) Change your test class to API version 24.0
  2. If you would rather stay in API 23.0 or lower, then, at the start of your test method, delete all accounts named 'Default Account', then insert your test accounts.