+ Start a Discussion
brianspatsobrianspatso 

Test Class not triggering Apex Trigger

And I though test classes were easy....

 

I am writing a Test Class for a Trigger which updates the count of a child object in the parent object. Except the Test Class does not seem to pass.

 

The parent and child are both Accounts, where the child record is identified by a lookup field to the parent record. When I change an Account to be a child of another Account, the trigger fires an update of the count in the parent record.

 

The test class runs a very simple test, which creates two Accounts (A and B), then updates the lookup in B to point to A. This should cause the Trigger to run, which would update the count on the parent Account, A.

 

However, the test class fails, as the count on the parent is still NULL.

 

I have performed the same test manually and it works - the count on the parent Account is definitely increased.

 

So why does the Test Class fail??

 

Here is the class:

@isTest
private class TestDepartmentCountChange {

static testMethod void testCountChange() {

Account companya = new Account(Name = 'Company A', RecordTypeId = '01220000000Dqq7');
insert companya;

Account companyb = new Account(Name = 'Company B', RecordTypeId = '01220000000Dqq7', Department__c = TRUE, Parent_Company__c = companya.id);
insert companyb;

//system.assert(companya.No_of_Departments__c == NULL);

//companyb.Department__c = TRUE;
//companyb.Parent_Company__c = companya.id;
//update companyb;

system.assert(companya.No_of_Departments__c == 1);

companyb.Department__c = FALSE;
companyb.Parent_Company__c = NULL;
update companyb;

system.assert(companya.No_of_Departments__c == NULL);

}
}

 And here is the Trigger:

trigger DepartmentSumTrigger2 on Account (after delete, after insert, after undelete,
after update) {

    // When deleting a Department use the previous values
    Account[] dept;
    if (Trigger.isDelete){
        dept = Trigger.old;
    }
  
    else
        dept = Trigger.new;
    

   
    // Get a list of all Accounts that are Parent Companies
    Set<ID> acctIds = new Set<ID>();
    for (Account dep :dept) {
        acctIds.add(dep.Parent_Company__c);
    }
    
    // Fill a Map with a list of all Departments
    Map<ID, Account> departmentsForAccounts = 
    new Map<ID, Account>([SELECT Id, Parent_Company__c FROM Account WHERE Department__c = TRUE AND Parent_Company__c IN :acctIds]);
    
    // Fill a map with a list of all Parent Companies
    Map<ID, Account> acctsToUpdate = 
    new Map<ID, Account>([SELECT Id, No_of_Departments__c FROM Account WHERE Id IN :acctIds]);
    
    // For all Parent Companies            
    for (Account acct : acctsToUpdate.values()) {
        Set<ID> depIds = new Set<ID>();
        for (Account dep : departmentsForAccounts.values()) {
            if (dep.Parent_Company__c == acct.Id)
                depIds.add(dep.Id);
        }
        if (acct.No_of_Departments__c != depIds.size())
            acct.No_of_Departments__c = depIds.size();
    }

    update acctsToUpdate.values();

}



Best Answer chosen by Admin (Salesforce Developers) 
DevGarethmdDevGarethmd

Execute a SOQL query on company a (making sure that you include the No_of_Departments__c field) just before you test each of your assertions.

All Answers

DevGarethmdDevGarethmd

Execute a SOQL query on company a (making sure that you include the No_of_Departments__c field) just before you test each of your assertions.

This was selected as the best answer
brianspatsobrianspatso

That worked, thanks a lot!

 

Here's the final code:

@isTest
private class TestDepartmentCountChange {

static testMethod void testCountChange() {

Account companya = new Account(Name = 'Company A', RecordTypeId = '01220000000Dqq7');
insert companya;

Account companyb = new Account(Name = 'Company B', RecordTypeId = '01220000000Dqq7', Department__c = TRUE, Parent_Company__c = companya.id);
insert companyb;

//system.assert(companya.No_of_Departments__c == NULL);

//companyb.Department__c = TRUE;
//companyb.Parent_Company__c = companya.id;
//update companyb;

Account[] a = [SELECT Id, No_of_Departments__c, Name FROM Account WHERE Name = 'Company A'];

for(Account firstCompany :a){
    system.assert(firstCompany.No_of_Departments__c == 1);
}

companyb.Department__c = FALSE;
companyb.Parent_Company__c = NULL;
update companyb;

system.assert(companya.No_of_Departments__c == NULL);

}
}