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
dan farrisdan farris 

aggregateResult always empty when trigger executed thru Test Case, works fine thru API.

Hi,  I have a trigger that works fine when I insert, update and delete opportunities thru the API.  When I run my test case the aggregateResult list is always empty.  Below is the code.  The code always executes the isEmpty code and won't cover the code below.  The trigger and test case worked fine before and is only having problems due to changes to get the campaign with the most recent close date..

list<aggregateResult> aggResults = [select max(closedate),campaign.name mycamp from opportunity where (campaign.name like 'R/%/NA' or campaign.name like 'R/%/Misc') 
and AccountId = :ContID group by campaign.name order by max(closedate) desc NULLS Last];

If (aggResults.isEmpty())
{
Account test4 = [SELECT Id, Last_Div_Campaign__c, Current_Campaign_Giving__c FROM Account WHERE Id = :ContID];
test4.Current_Campaign_Giving__c = 0;
test4.Last_Div_Campaign__c = null;
update test4;
return;
}


 
dan farrisdan farris
Test Case should read Test Class.
Andrew GAndrew G
Hi Dan

If the aggregate result is working correctly in the API, but not in your test Class, then I would strongly suspect that the test data is not sufficient / correct to satisfy the Select statement. 

Review your test class, in particular the data you are loading, and see if anything obvious jumps out.  Also maybe confirm that the :ContID is being set in the test data - either put a System.debug or run your test class in debug mode. (dependent on your IDE).  If running a debug log, see what the log says about the returned SOQL value.

You can also try posting it here as a second set of eyes is never a bad thing to help troubleshoot code.

Regards
Andrew