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

AggregateResult returns values when executing anonymously, but not when running a unit test
In this query contactsPerAccount.size() > 0 when executing anonymously. But the exact same query, exactly the same, returns 0 when running in an apex class. Why is this?
List<AggregateResult> contactsPerAccount = [select AccountId, count(Id) accountContacts from Contact group by AccountId]; system.debug ('contactsPerAccount size: ' + contactsPerAccount.size());
Please paste your class here and it will be easy to figure it out.
Regards,
Mahesh
12:13:55.0 (50758226)|VARIABLE_ASSIGNMENT|[5]|numContacts|{"s":1,"v":[{"s":2,"v":{"AccountId":"0014000000HJXxHAAX","accountContacts":1}}]}|0x5d1e42e9 12:13:55.0 (50763114)|STATEMENT_EXECUTE|[10] 12:13:55.0 (50768102)|HEAP_ALLOCATE|[10]|Bytes:18 12:13:55.0 (50885014)|HEAP_ALLOCATE|[10]|Bytes:1 12:13:55.0 (50900848)|HEAP_ALLOCATE|[10]|Bytes:19 12:13:55.0 (50922151)|USER_DEBUG|[10]|DEBUG|numContacts size: 1
12:13:55.0 (50934552)|METHOD_EXIT|[12]|01p40000000R0jc|contactsPerAccount.contactCounter(Map<Id,Contact>)
I got the results properly with your code.
If you want to do some modification to the current records then it will be in the bfore trigger.
If you want to do insert/update of other objects data then it will be in the after trigger.
Regards,
Mahesh