Ok, now the AccountProcessor class is working. Thanks to your help. But now I have a problem with the test method. For some reason it forgets the parameter I pass to the function and I have no clue why it does that nor what I would have to adapt to make it work. Here is my code:
AccountProcessor Class (working):
public class AccountProcessor {
@future
public static void countContacts(List<Id> recordIds){
List<Integer> ResultList = new List<Integer>(); // Liste ResultList initialisieren
Account[] AccountResultList = new List<Account>(); // Liste AccountResultList initialisieren
AggregateResult[] groupedResults = [Select count(Id) , AccountId from contact where AccountId in :recordIds group by AccountId]; // Aggregierte Liste mit Anzahl der Kontakte und der AccountID
// Die nachfolgende Schleife ermittelt basierend auf dem SQL die Felder und ordnet sie den gewünschten Listen zu
for (AggregateResult ar : groupedResults) { // "ValueOf" ermittelt den tatsächlichen Inhalt eines Objektfeldes
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);
}
// Output of determined lists
System.debug('ResultList'+ResultList);
System.debug('AccountResultList'+AccountResultList);
// Update of determined accounts according to List "AccountResultList"
try {
update AccountResultList;
} catch(DmlException e) {
System.debug('An unexpected error has occurred: ' + e.getMessage());
}
}
}
AccountProcessor Testclass:
@IsTest
public class AccountProcessorTest {
@IsTest
private static void testAcc_existing_Accounts() {
List<Id> my = new List<Id>(); // ID Liste initialisieren
Test.startTest();
my.add ('0010900000I0ajgAAB');
my.add ('0010900000IWBtHAAX');
my.add ('0010900000IWBtEAAX');
system.debug ('Running synchronous tests here...my: '+my);
AccountProcessor.countContacts(my);
Test.stopTest();
system.debug ('Running _asynchronous_ here...');
}
}
=> Problem: When checking the DEBUG-Logs:
ResultList () <== allways empty
AccountResultList () <== allways empty
I wanted to use the debug-log to see that the lists had been processed correctly. If I execute the AccountProcessor class without TEST-Context (directly over the anonymous execution) it works. It seems to be the Test-Class that is not correct or my approach is wrong. Anyone who can help?
Thanks in advance!