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
SidViciousSidVicious 

Help with AggregateResult Test Coverage for Apex

Hi guys, I've stumbled another obstacle - this time providing code coverage for AggregateResult. The general idea is to "count" the number of Invoice Line records under Invoice then put them inside a Map. I've only included a snippet of my code which I believe are relevant.

Apex class:
public override void afterDelete() {

        for(InvoiceLine__c invLine : (List<InvoiceLine__c>) Trigger.old){

            if(invLine.InvoiceId__c != null){

                invLineIdSet.add(invLine.InvoiceId__c);
            }
        }

        List<AggregateResult> invoiceLineList = [SELECT COUNT(Id) counter, InvoiceId__c 
                                                 FROM InvoiceLine__c 
                                                 WHERE InvoiceId__c =: invLineIdSet
                                                 GROUP BY InvoiceId__c];

        List<Invoice__c> invoiceList = [SELECT Id, OpportunityId__c FROM Invoice__c
                                        WHERE Id IN: invLineIdSet];

        Map<Id, Boolean> invLineChecker = new Map<Id, Boolean>();

        Boolean noLineRecords;

        for(Invoice__c invL : invoiceList){

            noLineRecords = true;

            invLineChecker.put(invL.Id, noLineRecords);

            oppIdSet.add(invL.OpportunityId__c);
        }

        for(AggregateResult invoiceLine : invoiceLineList){

            Integer counter = (Integer) invoiceLine.get('counter');

            Id parentInvoiceId = (Id) invoiceLine.get('InvoiceId__c');
            
            if(counter > 0){

                noLineRecords = false;

                invLineChecker.put(parentInvoiceId, noLineRecords);
            }else{

                noLineRecords = true;

                invLineChecker.put(parentInvoiceId, noLineRecords);
            }
        }

        System.debug('NO RECORDS? :' + invLineChecker);
}

As provided in the snippet above, I've highlighted the block of code where I'm unable to get code coverage. If somehow I'll be able to resolve this, I may figure out the rest.

Finally, below is my test class.

Test Class:
private static Opportunity__c opportunityRecord = new Opportunity__c();
private static List<InvoiceLine__c> invoiceLineList = new List<InvoiceLine__c>();

@isTest
    private static void deleteInvoiceLinesNoRecords(){

        initializeData();

        insert invoiceLineList;

        System.assertNotEquals(0, invoiceLineList.size());

        Test.startTest();

        	try
			{
                delete invoiceLineList;
			}Catch(Exception e)
            {
                String errormessage = e.getMessage();
                system.debug(errormessage);
            }

        Test.stopTest();
    }

@isTest
    private static void initializeData(){
        
        opportunityRecord = new Opportunity__c();
        opportunityRecord.Account__c = '0012x000002kyl7AAA'; // required field
        insert opportunityRecord;

        invoiceRecord = new Invoice__c();
        invoiceRecord.OpportunityId__c = opportunityRecord.Id; // required field
        insert invoiceRecord;

        InvoiceLine__c invoiceLineRecord1 = new InvoiceLine__c();
        invoiceLineRecord1.InvoiceId__c = invoiceRecord.Id; // required field

        InvoiceLine__c invoiceLineRecord2 = new InvoiceLine__c();
        invoiceLineRecord2.InvoiceId__c = invoiceRecord.Id; // required field

        InvoiceLine__c invoiceLineRecord3 = new InvoiceLine__c();
        invoiceLineRecord3.InvoiceId__c = invoiceRecord.Id; // required field

        invoiceLineList = new List<InvoiceLine__c>();
        
        invoiceLineList.add(invoiceLineRecord1);
        invoiceLineList.add(invoiceLineRecord2);
        invoiceLineList.add(invoiceLineRecord3);
    }

I would appreciate it greatly if anyone can address this issue with me.

What could I be missing here?

Thank you!

- Sid
Best Answer chosen by SidVicious
Maharajan CMaharajan C
Hi Sid,

You are deleting all the invoiceLine records in Test Method so obivisiouly the below query will return the 0 records. So delete the 1 or 2 record from list.

List<AggregateResult> invoiceLineList = [SELECT COUNT(Id) counter, InvoiceId__c  FROM InvoiceLine__c                                                   WHERE InvoiceId__c =: invLineIdSet  GROUP BY InvoiceId__c];

Try the below code :
 
private static void deleteInvoiceLinesNoRecords(){

        initializeData();

        insert invoiceLineList;
		
        // Query one record for delete
		List<InvoiceLine__c> delInvList = [Select Id from InvoiceLine__c limit 1];
		
		System.assertEquals(3, invoiceLineList.size());
        System.assertNotEquals(0, invoiceLineList.size());

        Test.startTest();

        	try
			{
                //delete invoiceLineList;
				delete delInvList;
			}Catch(Exception e)
            {
                String errormessage = e.getMessage();
                system.debug(errormessage);
            }

        Test.stopTest();
    }


Thanks,
Maharajan.C

All Answers

Abhishek BansalAbhishek Bansal
Hi Sid,

You are using after delete trigger which means that the data is already deleted from the database and you are trying to SOQL the same data from query which in theory doesn't exist at all.
Please change the trigger context to before delete and than try the same logic. It will work.
Let me know if you need any other help.

Thanks,
Abhishek Bansal.
Maharajan CMaharajan C
Hi Sid,

You are deleting all the invoiceLine records in Test Method so obivisiouly the below query will return the 0 records. So delete the 1 or 2 record from list.

List<AggregateResult> invoiceLineList = [SELECT COUNT(Id) counter, InvoiceId__c  FROM InvoiceLine__c                                                   WHERE InvoiceId__c =: invLineIdSet  GROUP BY InvoiceId__c];

Try the below code :
 
private static void deleteInvoiceLinesNoRecords(){

        initializeData();

        insert invoiceLineList;
		
        // Query one record for delete
		List<InvoiceLine__c> delInvList = [Select Id from InvoiceLine__c limit 1];
		
		System.assertEquals(3, invoiceLineList.size());
        System.assertNotEquals(0, invoiceLineList.size());

        Test.startTest();

        	try
			{
                //delete invoiceLineList;
				delete delInvList;
			}Catch(Exception e)
            {
                String errormessage = e.getMessage();
                system.debug(errormessage);
            }

        Test.stopTest();
    }


Thanks,
Maharajan.C
This was selected as the best answer