[SalesForce] Deploying an Apex Class for a Custom Button – Too Many DML Statements Error

I am attempting to deploy an Apex Class from a sandbox environment to a production environment of Salesforce and I am having issues with the deployment. I am very new at this, so please forgive my ignorance in apex coding.

Here is the portion of the apex class that is causing the deployment to fail due to not meeting the minimum code coverage requirement. The error message is telling me that the 3rd line of the code below is where the error is coming from.

static testMethod void getCasesTest() {
    for (Case obj : [select Id from Case]) {
        delete obj;

      String queueName = 'TestQueue1';
      Group queue = new Group(Type='Queue', Name=queueName);
      insert queue;
      QueueSobject sobj = new QueueSobject(QueueId = queue.Id, SobjectType = 'Case');
      insert sobj;
        Case case1 = new Case(Subject='TestCase1', OwnerId = queue.Id, V1Association__c = true);
        Case case2 = new Case(Subject='TestCase2', OwnerId = queue.Id, V1Association__c = true);
        Case case3 = new Case(Subject='TestCase3', OwnerId = queue.Id, V1Association__c = true);
        Case case4 = new Case(Subject='TestCase4', V1Association__c = true);
        insert new Case[] {case1, case2, case3, case4};

        Case[] cases = V1CaseCollector.getCases(queue.Id, false);
        System.assertEquals(3, cases.size(), 'Incorrect numbers of cases.');

        case2.status = 'Closed';
        case3.V1Association__c = false;
        update new Case[] {case2, case3};
        Case[] newCases = V1CaseCollector.getCases(queue.Id, true);
        System.assertEquals(1, newCases.size(), 'Incorrect numbers of cases. One cases have to be closed');
        assertContains(case2, newCases, 'Incorrect data in result.');
        newCases = V1CaseCollector.getCases(queue.Id, false);
        System.assertEquals(1, newCases.size(), 'Incorrect numbers of cases. Two classes have to be not closed');
        assertContains(case1, newCases, 'Incorrect data in result.');
        newCases = V1CaseCollector.getCases(null, false);
        System.assertEquals(2, newCases.size(), 'Incorrect numbers of cases.');

When I try to deploy the new Apex Class change set, I am getting the following error on this other test class:

API Name = V1CaseCollectorTestSuite.getCasesTest()
Type = Class
Line = 248
Column = 1
Problem = Failure Message: "System.Exception: Too many DML statements: 151", Failure Stack Trace: "Class.V1CaseCollectorTestSuite.getCasesTest: line 248, column 1"

Any ideas as to why I might be getting this error, and how I can go about correcting it so that I can correctly deploy my original change set?

Best Answer

Don't ever do DML or SOQL within an iteration scope. You can directly perform these on a collection object.

List<Case> lstToDelete = new List<Case>(); //COLLECTION
for (Case obj : [select Id from Case]) {
        //any possible business logic

        //collect objects you wish to delete/update/anything
        lstToDelete.add( obj); //COLLECT

        //never do another soql query for related records in an interation either, collect the id's in a collection object, and then query  as SELECT  fields FROM obj WHERE field IN:collection

delete lstToDelete;  //BULK EXECUTE

In all honesty, if this is your testcode, I'm a bit worried about your production code. Topics as bulkifying code and governer limits are essential to know when coding on the force.com platform.

Related Topic