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
Sakthi KuppusamySakthi 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
Best Answer chosen by Sakthi Kuppusamy
Abdul KhatriAbdul 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.
 
    @IsTest
    private static void testAccountProcessor()
    {
        string AcName = 'United Oil & Gas%';
        List<Contact> contToInsertList = new List<Contact>();
        
        Account acct = new Account(Name = AcName);
        insert acct;
               
        Contact contact1 = new Contact(FirstName = 'FN', LastName = 'LN', AccountId = acct.Id);
        Contact contact2 = new Contact(FirstName = 'FN2', LastName = 'LN2', AccountId = acct.Id);
        contToInsertList.add(contact1);
        contToInsertList.add(contact2);
        
        Insert contToInsertList;
        
        //Id myId1 = Id.valueOf('0016g000007qSglAAE');
        //Id myId2 = Id.valueOf('0016g000007qSgmAAE');
        List<Id> MyList =new List<Id>{contact1.Id, contact2.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());
    }