You need to sign in to do that
Don't have an account?
Somnath Paul 3
Test Class for Aggregate Result function
Hi,
Need help in writing Test Class for the below Apex Class which use AggregateResult.
Apex Code:
Test Class for the above:
Code Coverage: 50%. Below strike through codes do not fall under test coverage.
global class AccountProcessor {
@future
public static void countContacts(List<Id> AccntIds)
{
List<Account> list_of_Account = [select Id,sompaul__Number_of_Contacts__c from Account where Id IN: AccntIds];
system.debug('List of accounts'+list_of_Account);
List<Account> accountToBeUpdated = new List<Account>();
Integer Contact_cnt=0;
//Map<Id,Decimal> = new Map<Id,Decimal>();
List<AggregateResult> agrRes = [select count(Id),Contact.Account.Id from Contact,Contact.Account where Contact.Account.Id IN : AccntIds group by Contact.Account.Id];
system.debug('AggregateResult value:'+agrRes);
for(AggregateResult ar: agrRes)
{
for(Account a:list_of_Account)
{
if(a.Id == ar.get('Id'))
{
a.sompaul__Number_of_Contacts__c = (Decimal)ar.get('expr0');
accountToBeUpdated.add(a);
}
}
system.debug('Account Data to be updtaed'+accountToBeUpdated);
}
if(!accountToBeUpdated.isEmpty())
{
update accountToBeUpdated;
}
}
}
Can somebody please help here.
Thanks in advance !!
Need help in writing Test Class for the below Apex Class which use AggregateResult.
Apex Code:
global class AccountProcessor { @future public static void countContacts(List<Id> AccntIds) { List<Account> list_of_Account = [select Id,sompaul__Number_of_Contacts__c from Account where Id IN: AccntIds]; system.debug('List of accounts'+list_of_Account); List<Account> accountToBeUpdated = new List<Account>(); Integer Contact_cnt=0; //Map<Id,Decimal> = new Map<Id,Decimal>(); List<AggregateResult> agrRes = [select count(Id),Contact.Account.Id from Contact,Contact.Account where Contact.Account.Id IN : AccntIds group by Contact.Account.Id]; system.debug('AggregateResult value:'+agrRes); for(AggregateResult ar: agrRes) { for(Account a:list_of_Account) { if(a.Id == ar.get('Id')) { a.sompaul__Number_of_Contacts__c = (Decimal)ar.get('expr0'); accountToBeUpdated.add(a); } } system.debug('Account Data to be updtaed'+accountToBeUpdated); } if(!accountToBeUpdated.isEmpty()) { update accountToBeUpdated; } } }
Test Class for the above:
@IsTest private class AccountProcessorTest { private static testMethod void testAccountProcessor() { List<Id> accntList = new List<Id>(); accntList = createAccount(); Test.startTest(); AccountProcessor.countContacts(accntList); Test.stopTest(); } private static List<Id> createAccount() { List<Id> accntList = new List<Id>(); for(Integer i=0;i<10;i++) { Account a = new Account(Name='Test Account'+i); for(Integer j=0;j<5;j++) { Contact con = new Contact(LastName ='Test Contact'+j,AccountId=a.Id); } accntList.add(a.Id); } return accntList; } }
Code Coverage: 50%. Below strike through codes do not fall under test coverage.
global class AccountProcessor {
@future
public static void countContacts(List<Id> AccntIds)
{
List<Account> list_of_Account = [select Id,sompaul__Number_of_Contacts__c from Account where Id IN: AccntIds];
system.debug('List of accounts'+list_of_Account);
List<Account> accountToBeUpdated = new List<Account>();
Integer Contact_cnt=0;
//Map<Id,Decimal> = new Map<Id,Decimal>();
List<AggregateResult> agrRes = [select count(Id),Contact.Account.Id from Contact,Contact.Account where Contact.Account.Id IN : AccntIds group by Contact.Account.Id];
system.debug('AggregateResult value:'+agrRes);
for(AggregateResult ar: agrRes)
{
for(Account a:list_of_Account)
{
if(a.Id == ar.get('Id'))
{
a.sompaul__Number_of_Contacts__c = (Decimal)ar.get('expr0');
accountToBeUpdated.add(a);
}
}
system.debug('Account Data to be updtaed'+accountToBeUpdated);
}
if(!accountToBeUpdated.isEmpty())
{
}
}
}
Can somebody please help here.
Thanks in advance !!
All Answers
It works now.