function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jjvdevjjvdev 

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());
Mahesh DMahesh D
If you are using the With Sharing then it applies all sharing rules.

Please paste your class here and it will be easy to figure it out.

Regards,
Mahesh
Jack VolkovJack Volkov
trigger:
trigger ContactTrigger on Contact (before insert) {

map<Id, Contact> accountContactMap = new map<Id, Contact>();
	
	for(Contact c: trigger.new) {
		if (c.AccountId != null) {
			accountContactMap.put(c.AccountId,c);
		}
	}

       if (!accountContactMap.isEmpty()) {
            contactsPerAccount.contactCounter(accountContactMap);
        }
class:
public class contactsPerAccount {
    
    public static void contactCounter (Map<Id, Contact> triggerContacts) { 
        
        List<AggregateResult> numContacts = [select AccountId, count(Id) accountContacts
                                          	from Contact
                                          	where AccountId IN :triggerContacts.keySet()
                                          	group by AccountId];
		// this keeps returning 0
		system.debug ('numContacts size: ' + numContacts.size());

  }
    
}


 
Jack VolkovJack Volkov
this also returns 0:
public class contactsPerAccount {
    
    public static void contactCounter (Map<Id, Contact> triggerContacts) { 
        
        List<AggregateResult> numContacts = [select AccountId, count(Id) accountContacts
                                          	from Contact
                                          	// where AccountId IN :triggerContacts.keySet()
                                          	group by AccountId];
		// this keeps returning 0
		system.debug ('numContacts size: ' + numContacts.size());

  }
    
}

 
Mahesh DMahesh D
I tested it in my DE and below is the debug:

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.

 
Jack VolkovJack Volkov
How are you testing it?  This numContacts returns 0 when I execute this:
Account a = new Account(Name='Test Company', Website='www.example.com', Phone='8885551234', Type='ZipRecruiter Customer', Org_ID__c='abdefgh');
insert a;
        
Contact con = new Contact(AccountId=a.Id,LastName='Thistest',Phone='8885551234',Email='test@example.com');
insert c;


 
Jack VolkovJack Volkov
Ah, but executing this and numContacts returns 1.  Maybe it is something with inserting the account and the contact during the same execution?
 
Account a = [select id from account limit 1];
        
Contact c = new Contact(AccountId=a.Id,LastName='ThisTest',Phone='8885551234',Email='test@example.com');
insert con;

 
Jack VolkovJack Volkov
Changing to an after trigger fixed this problem for me.
Mahesh DMahesh D
I just took your trigger and class. Copied in my DE org and verified the debug.
Mahesh DMahesh D
It depends on your actual requirement.

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