I am trying to Create test data with more than 10000 rows dynamically in a test class. Am hitting the dml row limit error (Too Many DML Rows: 10001). I dont want to use loaddata feature as I need to manually populate values in excel for that. I want my data to be apex driven. Any suggestions?

I have removed my dynamic logic and pasted some sample code here :

@testSetup static void createTestRecords(){  

        List<MyObject__c> myObjects = new List<MyObject__c>();
        for(Integer i=1;i<=10000;i++){
            myObjects.add(new MyObject__c());
        insert myObjects;
  • You can use Batch class to insert more than 10k records.. – Himanshu jasuja Apr 27 '16 at 12:38
  • 1
    Is there a particular reason why you need to generate so many test records? If it were possible, such a test would take a long time to run. If you need to verify that your system under test doesn't utilize more than X amount of (insert governor limit here), you'd be much better served by running 1 or 2 trigger batches (200 recs) and making use of the System.Limits class – Derek F Apr 27 '16 at 13:13
  • I have been given a requirement to ensure that my system handles 10K records for 1 object, and 50K records for another object, irrespective of environments, as we have multiple environments. So I am trying to write everything in test code instead of loading some csv and invoking Test.loadData() – Sathya Apr 29 '16 at 5:47
  • @Himanshujasuja can I ask how would you use batch? I thought excecute method of the batch is through the records that were query in the Database.QueryLocator start, if this doesnt return anything (I am guessing in this case) the excecute will never start? thank you for anny clarification – manza May 13 at 0:07

The simplest solution would be to insert 5000 in one pass, and then use the Test.Starttest(), which will give you a fresh set of limits.

Using a batch won't solve the problem directly in a test class, but you could use a batch outside of a the test method. Then, run the test with "seeAllData= true". Its not ideal, but that approach could give you many thousands of records to test with.

| improve this answer | |
  • But if we do a startTest and stopTest, will the data be visible across each pass? Because I have to insert 5 different objects. 3 with 100 records, 1 with 10000 records, another with 50000 records to test the volume my triggers are able to handle. So after creating these records. If I perform updates on the records created in different passes using startTest and stopTest, do you think those records will be visible during the update finally in my test method which would actually invoke the trigger? – Sathya Apr 28 '16 at 6:07
  • Yes, records created before "startTest" are visable after startTest -- within the same test method. – SeanW Apr 29 '16 at 14:40
  • Or, consider that what you are doing is not a "coverage test", it is a functional test,. If you reframe it that way, you would have the opportunity to exercise the functionality in a regular Apex class, without the constraints imposed by test methods. – SeanW Apr 29 '16 at 14:47
  • But am unable to add more than one Test.startTest in a method. So this method doesnt work out. – Sathya May 4 '16 at 7:20
  • I'll try to clarify. If you 12,000 records, you can insert 6,000 and then do Test.startTest, and then insert another 6,000. That addresses the issue in the original question, which asked about 10,000 records. If you need 50,000 you really cannot create them within the execution of a test method. You could create them before the test method, and then execute the test using 'seeAllData = true'. – SeanW May 4 '16 at 14:22

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.