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
Andrea Paola Contrera VelandiaAndrea Paola Contrera Velandia 

System.QueryException: Non-selective query against large object type (more than 200000 rows)

Hi guys,

I'm having troubles with this error when i try to do the following query inside a trigger. 

 public static Map<String, AggregateResult> getTotalDesembolsos(Set<Integer> setAnios, Set<Id> setEjecutivoIds) {
        System.debug('\n\n-=#=-\n>>>>>>>>>>   ' + 'CRM_DesembolsoTriggerHandler_cls - getTotalDesembolsos' + '   <<<<<<<<<<\n' +
                     'setAnios' + ': ' + setAnios + '\n' +
                     'setEjecutivoIds' + ': ' + setEjecutivoIds + '\n-=#=-\n');

        Map<String, AggregateResult> mapCantidadDesembolsos = new Map<String, AggregateResult>();

        for(AggregateResult objAggregateResultsDesembolsos : [SELECT CALENDAR_YEAR(CRM_FechaInicio__c)      Anio
                                                                    ,CRM_EjecutivoComercial__c              Ejecutivo
                                                                    ,COUNT(Id)                              Desembolsos
                                                                    ,COUNT_DISTINCT(CRM_Cliente__c)         Clientes
                                                                    ,SUM(CRM_DesembolsoTotalReexpresado__c) TotalDesembolsos
                                                                FROM CRM_Desembolso__c
                                                               WHERE CALENDAR_YEAR(CRM_FechaInicio__c)  IN: setAnios
                                                                 AND CRM_EjecutivoComercial__c          IN: setEjecutivoIds
                                                                 AND CRM_Cliente__c                     !=  NULL
                                                            GROUP BY CALENDAR_YEAR(CRM_FechaInicio__c)
                                                            ORDER BY CALENDAR_YEAR(CRM_FechaInicio__c)
                                                                    ,CRM_EjecutivoComercial__c]) {
            System.debug('\n\n-=#=-\n' + 'objAggregateResultsDesembolsos' + ': ' + objAggregateResultsDesembolsos + '\n-=#=-\n');

            mapCantidadDesembolsos.put((Integer)objAggregateResultsDesembolsos.get('Anio') + '-' + (String)objAggregateResultsDesembolsos.get('Ejecutivo'), objAggregateResultsDesembolsos);

I read someting about Custom Index but I don't know how request this custom index. If someone know something i will appreciate.

Amit Singh 1Amit Singh 1
A couple ways to get more CPU time are to put the code in an @future method (you get 60 seconds of CPU instead of just 10 seconds), or implement it as a Batch Apex job.  Since both of these methods are asynchronous, the trigger and associated DML will complete before the calls to the AccountServices methods are made - but they will be called eventually.  Would this work for you?

Also, refer the below

Amit Singh
Arunkumar RArunkumar R

Please see the below help reference. It will help you to resolve this issue.

Make sure and remember the below notes when using Aggregate Query,

Queries that include aggregate functions are subject to the same governor limits as other SOQL queries for the total number of records returned. This limit includes any records included in the aggregation, not just the number of rows returned by the query. If you encounter this limit, you should add a condition to the WHERE clause to reduce the amount of records processed by the query.

Custom index request​ reference URL with steps,​