You need to sign in to do that
Don't have an account?
Sakthi Kuppusamy
Apex SOQL with AggregateResult
Hello, I am playing with @future method with below code
public class AccountProcessor {
@future
public static void countContacts(List<Id> recordIds) {
string AcName = 'United Oil & Gas%';
List<AggregateResult> ct_contacts = [Select AccountId, Count(Name) from Contact Where Account.Name Like :AcName group by AccountId ];
List<Account> Upd_Account = new List<Account>();
system.debug('Count - ' + ct_contacts.size());
for (AggregateResult ar : ct_contacts) {
Account acc = new Account();
acc.Id = ((Id)ar.get('AccountId'));
acc.Number_of_Contacts__c = ((Integer)ar.get('expr0'));
Upd_Account.add(acc);
}
update Upd_Account;
}
}
and below test calss
@IsTest
public class AccountProcessorTest {
@IsTest
private static void testAccountProcessor() {
Id myId1 = Id.valueOf('0016g000007qSglAAE');
Id myId2 = Id.valueOf('0016g000007qSgmAAE');
List<Id> MyList =new List<Id>();
MyList.add(myId1);
MyList.add(myId2);
Test.startTest();
AccountProcessor.countContacts(MyList);
Test.stopTest();
// runs callout and check results
List<AggregateResult> logs = [Select AccountId, Count(Name) from Contact Where AccountId IN :MyList group by AccountId];
System.assertEquals(0, logs.size());
}
}
My code is not gettign 100% coverage i treid removing @future as well but it is not executing the SOQL..
Select AccountId, Count(Name) from Contact Where Account.Name Like :AcName group by AccountId
I beleive it is not able to substitute the variable.
14:00:17:006 SOQL_EXECUTE_BEGIN [4]|Aggregations:0|SELECT AccountId, COUNT(Name) FROM Contact WHERE Account.Name LIKE :tmpVar1 GROUP BY AccountId
Is there any lilitation for using AggregateResult?
Thanks,
Sakthi
public class AccountProcessor {
@future
public static void countContacts(List<Id> recordIds) {
string AcName = 'United Oil & Gas%';
List<AggregateResult> ct_contacts = [Select AccountId, Count(Name) from Contact Where Account.Name Like :AcName group by AccountId ];
List<Account> Upd_Account = new List<Account>();
system.debug('Count - ' + ct_contacts.size());
for (AggregateResult ar : ct_contacts) {
Account acc = new Account();
acc.Id = ((Id)ar.get('AccountId'));
acc.Number_of_Contacts__c = ((Integer)ar.get('expr0'));
Upd_Account.add(acc);
}
update Upd_Account;
}
}
and below test calss
@IsTest
public class AccountProcessorTest {
@IsTest
private static void testAccountProcessor() {
Id myId1 = Id.valueOf('0016g000007qSglAAE');
Id myId2 = Id.valueOf('0016g000007qSgmAAE');
List<Id> MyList =new List<Id>();
MyList.add(myId1);
MyList.add(myId2);
Test.startTest();
AccountProcessor.countContacts(MyList);
Test.stopTest();
// runs callout and check results
List<AggregateResult> logs = [Select AccountId, Count(Name) from Contact Where AccountId IN :MyList group by AccountId];
System.assertEquals(0, logs.size());
}
}
My code is not gettign 100% coverage i treid removing @future as well but it is not executing the SOQL..
Select AccountId, Count(Name) from Contact Where Account.Name Like :AcName group by AccountId
I beleive it is not able to substitute the variable.
14:00:17:006 SOQL_EXECUTE_BEGIN [4]|Aggregations:0|SELECT AccountId, COUNT(Name) FROM Contact WHERE Account.Name LIKE :tmpVar1 GROUP BY AccountId
Is there any lilitation for using AggregateResult?
Thanks,
Sakthi
Best Answer chosen by Sakthi Kuppusamy
Abdul Khatri
No, First of all never hard code anything specially the 18 characters Id. Best practise is that you need to build you test data and then use that as I am showing you in this example. I have commented your code instead of deleting them so that you can see the difference. This should give you 100% code coverage.