APEX Test Class Failures in Production?

Question:
I’m trying to deploy an additional field into an existing Aura component bundle in my production org to improve the user interface. However, I keep encountering APEX test failures on the following test classes: CalculateexchangerateTest
and UpdatePBTest
. I am not proficient in APEX and did not build these test classes, as I mostly work with SQL and flows. Because of this, I can’t determine how to resolve these failures.
The error messages indicate a System.LimitException due to exceeding the DML row limit of 10,000:
System.LimitException: Too many DML rows: 10001
Stack Trace: Class.Calculateexchangerate.CalculateProductPrice: line 20, column 1
Class.CalculateexchangerateTest.TestProduct: line 10, column 1
and
System.LimitException: Too many DML rows: 10001
Stack Trace: Class.UpdatePB.UpdatePriceBook: line 49, column 1
Class.UpdatePBTest.TestProduct: line 10, column 1
Here are the test classes:
@isTest(seeAllData='true')
public class CalculateexchangerateTest {
static testMethod void TestProduct() {
Id standardPbid = Test.getStandardPricebookId();
Pricebook2 objPricebook2 = new Pricebook2(Name = 'price book 1', User__c = UserInfo.getUserId());
insert objPricebook2;
Product2 objProduct1 = new Product2(Name='Product 1', Family='Boat', Capacity__c=20, Product_Price__c=50);
insert objProduct1;
Product2 objProduct2 = new Product2(Name='Product 1', Family='Boat', Capacity__c=20, Product_Price__c=50, AUD_Product__c = objProduct1.Id);
insert objProduct2;
String ProductString = Calculateexchangerate.CalculateProductPrice();
}
}
@isTest(seeAllData='true')
public class UpdatePBTest {
static testMethod void TestProduct() {
Id standardPbid = Test.getStandardPricebookId();
Pricebook2 objPricebook2 = new Pricebook2(Name = 'price book 1', User__c = UserInfo.getUserId());
insert objPricebook2;
Product2 objProduct1 = new Product2(Name='Product 1', Family='Boat', Capacity__c=20, Product_Price__c=50);
insert objProduct1;
Product2 objProduct2 = new Product2(Name='Product 1', Family='Boat', Capacity__c=20, Product_Price__c=50, AUD_Product__c = objProduct1.Id);
insert objProduct2;
String ProductString = UpdatePB.UpdatePriceBook();
}
}
I suspect these failures are due to bulkification issues, but I am unsure how to resolve them. Additionally, these test classes are not available in my sandbox, so I don’t know how they were deployed. Since I cannot modify metadata in an active production org, what workarounds or solutions can I use to fix these failures?
Answer:
The error is caused by hitting the governor limit of 10,000 DML rows in a single transaction. This suggests that CalculateProductPrice()
and UpdatePriceBook()
are performing bulk DML operations without proper handling. Since seeAllData=true
is used in the test classes, they are interacting with live org data, which might contain a large number of records.
Boost your career with CRS Info Solutions’ Salesforce online training, offering expert guidance and practical experience. Master the Salesforce ecosystem through comprehensive, hands-on learning.
Possible Solutions
1. Refactor the Apex Classes to Handle Bulk Data Efficiently
If you have access to the Apex classes, check if DML operations are performed inside loops and refactor them to use bulk processing. Instead of inserting or updating records one by one, store them in a list and perform a single DML operation.
Example:
List<Product2> productsToUpdate = new List<Product2>();
for (Product2 p : productList) {
p.SomeField__c = 'New Value';
productsToUpdate.add(p);
}
if (!productsToUpdate.isEmpty()) {
update productsToUpdate;
}
2. Modify the Test Classes to Limit Data Usage
If you cannot modify the Apex classes but can edit the test classes, avoid seeAllData=true
. This setting allows access to production data, which may contain thousands of records. Instead, create only the necessary test data within the test class.
Example:
@isTest
public class CalculateexchangerateTest {
static testMethod void TestProduct() {
Pricebook2 objPricebook2 = new Pricebook2(Name = 'Test Pricebook');
insert objPricebook2;
Product2 objProduct1 = new Product2(Name='Test Product', Family='Boat', Capacity__c=20, Product_Price__c=50);
insert objProduct1;
Test.startTest();
String productString = Calculateexchangerate.CalculateProductPrice();
Test.stopTest();
}
}
3. Run Tests in an Isolated Developer Org
If the test classes are missing from your sandbox, retrieve them from production using Salesforce CLI or retrieve the metadata using the force:source:retrieve
command. Run the tests in an isolated sandbox to analyze failures without affecting production.
sf project retrieve start -m ApexClass -o myOrg
4. Deploy a Fix Using an Unmanaged Package or Change Set
Since you cannot modify metadata directly in production, you may need to deploy updated test classes through an unmanaged package, change set, or VS Code with the Salesforce CLI.
5. Check if a Batch Apex or Asynchronous Process Can Be Used
If the Apex classes perform large DML operations, consider using Batch Apex to process records in chunks rather than hitting the governor limit in a single transaction.
Master Salesforce with Expert Training in Hyderabad
Kickstart your career with our comprehensive Salesforce training in Hyderabad. Whether you’re new to Salesforce or looking to enhance your skills, our program offers specialized tracks in Salesforce Administration, Development, and Artificial Intelligence. We equip you with the knowledge and hands-on experience necessary to excel in the tech industry.
Our training program includes interactive modules, real-world projects, and expert-led sessions to ensure you’re fully prepared for certification exams and interviews. Our experienced instructors provide personalized attention, making even complex concepts easy to understand.
Start your journey to success today! Enroll for the free demo and set yourself on the path to a rewarding future!!!
Related Posts:
Writing Test Classes for Invocable and @Future Apex Methods
Salesforce Apex Testing Best Practices.