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 > ();
                    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];

                  //Duplication the records
                   Set<Clientes_Representantes__c> setCliRep = new Set<Clientes_Representantes__c>();
                    List<Clientes_Representantes__c> CliRepAInserir = new List<Clientes_Representantes__c>();

                        //upsert clientesRepresentantes;
                        insert CliRepAInserir;
  • are you doing all these operation in a loop? I am seeing only two 3 DML operations...(a query, a delete and an insert) – m Peixoto Dec 17 '18 at 11:00
  • Also, if it is not a loop, do you have triggers on these objects? It should be the issue – m Peixoto Dec 17 '18 at 11:08
  • why would you want to delete everything and then re-insert everything? – Novarg Dec 17 '18 at 11:10
  • 1
    @mPeixoto, the issue is probably the amount of records, not a loop. Also, a query would not count toward Too many DML rows limit, but rather Too many SOQL rows, which is 50000 – Novarg Dec 17 '18 at 11:11
  • So, in this code, there is only two DMLs... I am not sure about the approach to delete and reinsert and so on. So, I am trying to understanding why the error. – m Peixoto Dec 17 '18 at 11:14

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.

  • I created a batch and it worked perfectly. Thank you! – Daniela Cerqueira Dec 17 '18 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.