[SalesForce] Error: Too many DML rows: 10001

I have a question here, every time I run my code I first delete everything that is in the object, and then inserted a new list, when I try to do this simultaneously with the error ERROR System.LimitException: Too many DML rows: 10001 Do you know if there is any way to control this?

List < Clientes_Representantes__c > clientesRepresentantesExistentes = [SELECT Id FROM Clientes_Representantes__c];
                    if(clientesRepresentantesExistentes.size() > 0){
                        delete clientesRepresentantesExistentes;
                    }

                    List < Clientes_Representantes__c > clientesRepresentantes = new List < Clientes_Representantes__c > ();
                    System.debug('cod_cliList.size();'+cod_cliList.size());
                    for (Integer i = 0; i < cod_cliList.size(); i++) {
                        Clientes_Representantes__c clienteRepresentante = new Clientes_Representantes__c();
                        clienteRepresentante.Chave_Import__c = cod_chimportList[i];
                        clienteRepresentante.Numero_Cliente__c = cod_cliList[i];
                        clienteRepresentante.Numero_Representante__c = cod_repList[i];
                        clientesRepresentantes.add(clienteRepresentante);
                       System.debug(clienteRepresentante);
                    }

                  //Duplication the records
                   Set<Clientes_Representantes__c> setCliRep = new Set<Clientes_Representantes__c>();
                    List<Clientes_Representantes__c> CliRepAInserir = new List<Clientes_Representantes__c>();
                    setCliRep.addAll(clientesRepresentantes);
                    CliRepAInserir.addAll(setCliRep);
                    System.debug('CliRepAInserir'+CliRepAInserir);

                    if(!isTest){
                        System.debug('clientesRepresentantes'+clientesRepresentantes);
                        //upsert clientesRepresentantes;
                        insert CliRepAInserir;
                    }

Best Answer

This is a per-transaction limit. You'll need to break your code apart by using Batchable, Queueable or some other technique to stay under the 10,000 DML rows per transaction limit. There's way to break this limit under normal conditions. A transaction includes all the code that runs from start to finish in a single request, so if you're deleting a lot of records, that can cause you to reach this limit.

Related Topic