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
koppinjokoppinjo 

Failing prod test prevents deployment of a single class with 100% code coverage

I wrote a simple class, and have a test method which in the Force.com IDE is telling me covers this class 100%.  When I right-click on the class in the Package Explorer, go to Force.com > Deploy to server... I enter my production credentials and they are validated, then I see that only my class is set to add to the production environment and I go forward with the deployment.

After several minutes, I get a FAILURE message related to one test along the lines of 'too many DML rows', looking into the logs I see there are 10283 rows which exceeds the limit of 10000.  I log in to the production environment and run the failing test, and it fails in production too WITH THE SAME ERROR.

Now I have a chicken/egg situation and I don't know how to get any code to production with this failing test, and furthermore, I don't know how anything that would have broken this test would have made it to production!  I tried locally commenting out everything in the test class and the deployment failed in the exact same way (to the character) so I know it is not anything locally that I need to change.  I did have to fix some things in the test to get it to run locally, but that is irrelevant here especially since I commented the entire body out and got the same error.

HELP!!

Class I'm trying to deploy:

public class AuthorizationToken {

public String LoginId;

public String Password;

public AuthorizationToken(String user, String pw)

{

Password = pw;

LoginId = user;

}

statictestMethodvoid testAuthTokenInstantiation()

{

String user = 'testUser';

String pw = 'testPw';

Test.startTest();

AuthorizationToken testAuthToken = new AuthorizationToken(user, pw);

Test.stopTest();

System.assertEquals(testAuthToken.LoginId, 'testUser');

}

}

FAILING TEST CLASS:

@isTest
public with sharing class generateRenewalOppty_TEST
{
    static testMethod void myTest()
    {
        Boolean success = true;
       
       
           
            Account testAccount = new Account();
            testAccount.Name = 'Test';
            testAccount.Phone = '1111111111';
            testAccount.County__c = 'Macomb';
            testAccount.Member_Payment_Form__c ='Standard - Cash';
            testAccount.Type = 'Membership - New';
        
            insert testAccount;
           
            update testAccount;
           
            Product2 testProduct2 = new Product2(Name='TestProduct', ProductCode = '123', IsActive = true);
            insert testProduct2;
           
            List<Pricebook2> testPB2List = [select Id from Pricebook2 where IsStandard = true];
           
            PricebookEntry testPBE = new PricebookEntry(Product2Id = testProduct2.Id, Pricebook2Id = testPB2List[0].Id, UnitPrice = 5.0, UseStandardPrice = false, IsActive = true);
            insert testPBE;
           
           
            Opportunity oppObj = new Opportunity(Name='Test Opp',StageName='Closed Won - In-Kind',CloseDate=System.Today(),AccountId=testAccount.Id, type='Membership - New');
            insert oppObj;
           
            OpportunityLineItem testOPL = new OpportunityLineItem(OpportunityId = oppObj.Id, Quantity = 1.0, TotalPrice = 100, PricebookEntryId = testPBE.Id);
            insert testOPL;
           
            OpportunityLineItem testOPL1 = new OpportunityLineItem(OpportunityId = oppObj.Id, Quantity = 1.0, TotalPrice = 100, PricebookEntryId = testPBE.Id);
            insert testOPL1;
           
        
            testAccount.Generate_Renewal_Oppty__c = true;
            update testAccount;
          
            Opportunity[] oppOpen =[Select Id,Amount from Opportunity Where (StageName='Open' or StageName='Membership - Renewal') and AccountId =:testAccount.Id];
            System.assertEquals(1, oppOpen.size());
          
            OpportunityLineItem[] oppLi =[Select Id,TotalPrice from OpportunityLineItem Where OpportunityId=:oppOpen[0].Id];
            System.assertEquals(2, oppLi.size());
            System.assertEquals(100, oppLi[0].TotalPrice);
            System.assertEquals(100, oppLi[1].TotalPrice);
           
            Opportunity[] oppRec = [Select Id from Opportunity];
            delete oppRec;
           
            Opportunity oppOb = new Opportunity(Name='Test Opp1',StageName='Open',CloseDate=System.Today(),AccountId=testAccount.Id);
            insert oppOb;
           
            testAccount.Generate_Renewal_Oppty__c = true;
            update testAccount;
           
            Opportunity[] oppRec1 = [Select Id from Opportunity];
THIS IS LINE 52: delete oppRec1;
           
            Opportunity oppOb1 = new Opportunity(Name='Test Opp1',StageName='Open',CloseDate=System.Today(),AccountId=testAccount.Id,Type = 'Membership - New');
            insert oppOb1;
           
            testAccount.Generate_Renewal_Oppty__c = true;
            update testAccount;
           
            //delete oppOpen;
                      
        }
 
}


TEST RESULT DETAIL:

Class: generateRenewalOppty_TEST

Method Name: myTest

Pass/Fail: Fail

Error Message: System.LimitException: Too many DML rows: 10001

Stack Trace: Class.generateRenewalOppty_TEST.myTest: line 52, column 1

 

Best Answer chosen by Admin (Salesforce Developers) 
koppinjokoppinjo

RESOLVED

 

I was able to add some WHERE criteria to the above queries in this thread which were specific to the test (good practice anyone?).  I then deployed the updated/fixed test class (success), then I deployed my new class with 100% test coverage and it was successful:

 

Opportunity[] oppRec = [Select Id from Opportunity Where AccountId=:testAccount.Id And CloseDate=:System.Today()];
delete oppRec;

Opportunity[] oppRec1 = [Select Id from Opportunity Where AccountId=:testAccount.Id And CloseDate=:System.Today()];
delete oppRec1;

 Thanks to everyone who helped out on this!

All Answers

sfdcfoxsfdcfox
How the code got in there to begin with? You obviously had less than 10000 rows of live data that the query pulled when the code originally deployed.

There really isn't a chicken/egg situation here. You simply need to fix the offending query, then re-deploy the fixed code and your 100% covered code (you can do this all in a single step).

Unfortunately, you can't just ignore the error, so you'll have to spend the time fixing the code.
CaptainObviousCaptainObvious

I agree with sfdcfox...

 

I'd start by adding some WHERE clauses to the following queries:

 

 

Opportunity[] oppRec = [Select Id from Opportunity];

Opportunity[] oppRec1 = [Select Id from Opportunity];

 

koppinjokoppinjo

Thanks so much for the quick response! To offer some additional context, I am building a page withing a client's SF environment so I'm not intimately familiar with the environment, and this test was written by another contracting house that the client was using.

 

I've only been learning/using Apex for ~3 weeks but one thing that stands out is that the lines:

 

Opportunity[] oppRec = [Select Id from Opportunity];

delete oppRec;

Opportunity[] oppRec1 = [Select Id from Opportunity];

delete oppRec1;

 

do not have any WHERE clauses so this I would imagine is balooning up the # of DML rows. I am going to attempt to put some additional restrictions on the data requests and will post here if it resolves.

 

Thanks again,

Joe

koppinjokoppinjo
Thanks, you posted this while I was writing a similar observation below :)
sfdcfoxsfdcfox
You can probably safely remove the unfiltered query lines and their associated dml commands. In the worst case scenario, it seems like they're trying to delete the opportunities that were created in the test, not ALL opportunities.
dphilldphill
What version is your code? You might be using an old version which will bring back all existing data in the database. If you can only bring back data you create, it should fix your issue.
koppinjokoppinjo

RESOLVED

 

I was able to add some WHERE criteria to the above queries in this thread which were specific to the test (good practice anyone?).  I then deployed the updated/fixed test class (success), then I deployed my new class with 100% test coverage and it was successful:

 

Opportunity[] oppRec = [Select Id from Opportunity Where AccountId=:testAccount.Id And CloseDate=:System.Today()];
delete oppRec;

Opportunity[] oppRec1 = [Select Id from Opportunity Where AccountId=:testAccount.Id And CloseDate=:System.Today()];
delete oppRec1;

 Thanks to everyone who helped out on this!

This was selected as the best answer
sfdcfoxsfdcfox
That should be selective enough to avoid any problems. You could have also used the exact opportunity ID values, or, since the data was inserted originally, you could avoid the query entirely and just call delete on the variables that contain the opportunities to delete.