I solved this using this part of code:
@future
public static void countContacts(List<Id> recordIds){
System.debug('01 Predefining Resultlists');
List<Integer> ResultList = new List<Integer>(); // Liste ResultList initialisieren
Account[] AccountResultList = new List<Account>(); // Liste AccountResultList initialisieren
System.debug('02 Getting SOQL results for:'+recordIds);
AggregateResult[] groupedResults = [Select count(Id) , AccountId from contact where AccountId in :recordIds group by AccountId];
...
The testclass looks like this:
...
my.add ('0010900000I0ajgAAB');
my.add ('0010900000IWBtHAAX');
my.add ('0010900000IWBtEAAX');
system.debug ('Running synchronous tests here...my: '+my);
system.debug ('----------------------------------------');
Test.startTest();
AccountProcessor.countContacts(my);
Test.stopTest();
...
If I start the future method using the anonymous call it works and processes the entries.
If I access the method using the test class the "groupedResults"-List is allways empty.
It seems that SOQL queries at all return no results if used in conjunction with a unit-test.
What am I missing what am I doing wrong and what is correct? Maybe a timing problem?
Here is the complete code:
------------------------------------
public class AccountProcessor {
/*
* The method counts the number of Contact records associated to each
* Account ID passed to the method
* and updates the 'Number_Of_Contacts__c' field with this value
*/
@future
public static void countContacts(List<Id> recordIds){
System.debug('01 Predefining Resultlists');
List<Integer> ResultList = new List<Integer>(); // Liste ResultList initialisieren
Account[] AccountResultList = new List<Account>(); // Liste AccountResultList initialisieren
System.debug('02 Getting SOQL results for:'+recordIds);
AggregateResult[] groupedResults = [Select count(Id) , AccountId from contact where AccountId in :recordIds group by AccountId]; // Aggregierte Liste mit Anzahl der Kontakte und der AccountID
System.debug('02b Getting grouped SQL results'+groupedResults);
// Die nachfolgende Schleife ermittelt basierend auf dem SQL die Felder und ordnet sie den gewünschten Listen zu
System.debug('03 Starting Evaluating SOQL');
for (AggregateResult ar : groupedResults) { // "ValueOf" ermittelt den tatsächlichen Inhalt eines Objektfeldes
System.debug('03a ... Processing inside loop');
Integer currentNoOfContacts = Integer.ValueOf(ar.get('expr0'))*1; // Aktuelle Anzahl an Elementen in Integer umwandeln
String currentAccountId = String.ValueOf(ar.get('AccountId')); // Aktuelle AccountId aus Objekt ermittelnn und in String umwandeln
System.debug('AccountId: ' + currentAccountId); // Aktuelle AccountID ausgeben
System.debug('Number of contacts: ' + currentNoOfContacts); // inkl. der Anzahl damit verbundener Kontakte
ResultList.add(currentNoOfContacts); // Füge die Aktuelle Anzahl der Kontakte der Liste ResultList zu
// Da es sich bei "Account" um ein Object handelt muss dieses instanziiert werden um dessen Felder ändern zu können!
Account a = new Account(Id=currentAccountId, // Füge die aktuelle AccountId
Number_Of_Contacts__c=currentNoOfContacts); // die Anzahl der Kontakte der Liste AccountResultList hinzu
AccountResultList.add(a);
}
// Ausgabe der ermittelten Listen
System.debug('04 ResultList'+ResultList);
System.debug('05 AccountResultList'+AccountResultList);
try {
//update AccountResultList;
} catch(DmlException e) {
System.debug('An unexpected error has occurred: ' + e.getMessage());
}
}
}
Testclass:
@IsTest
public class AccountProcessorTest {
@IsTest
private static void testAcc_existing_Accounts() {
List<Id> my = new List<Id>(); // ID Liste initialisieren
my.add ('0010900000I0ajgAAB');
my.add ('0010900000IWBtHAAX');
my.add ('0010900000IWBtEAAX');
system.debug ('Running synchronous tests here...my: '+my);
system.debug ('----------------------------------------');
Test.startTest();
AccountProcessor.countContacts(my);
Test.stopTest();
system.debug ('Running _asynchronous_ here...');
system.debug ('----------------------------------------');
}
}
3 件の回答
Hi,
I solved the challenge by writing the below code:
Apex Class:
public class AccountProcessor {
@future
public static void countContacts(Set<id> setId) {
List<Account> lstAccount = [select id,Number_of_Contacts__c , (select id from contacts ) from account where id in :setId ];
for( Account acc : lstAccount ) {
List<Contact> lstCont = acc.contacts ; acc.Number_of_Contacts__c = lstCont.size();
}
update lstAccount;
}
}
Test Class:
@IsTest
public class AccountProcessorTest {
public static testmethod void TestAccountProcessorTest() {
Account a = new Account();
a.Name = 'Test Account';
Insert a;
Contact cont = New Contact();
cont.FirstName ='Bob'; cont.LastName ='Masters';
cont.AccountId = a.Id;
Insert cont; set<Id> setAccId = new Set<ID>();
setAccId.add(a.id);
Test.startTest();
AccountProcessor.countContacts(setAccId);
Test.stopTest();
Account ACC = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1];
System.assertEquals ( Integer.valueOf(ACC.Number_of_Contacts__c) ,1);
}
}
Hope this helps!