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
Ankit Singh 6Ankit Singh 6 

Getting random error while deploying changes from Sandbox to Production

Hi 

I am trying to deploy few of my code changes (related to trigger) but while deploying from Sandbox to Production I am facing error related to some test classes. Below is the error and code for these test classes.

System.LimitException: LeadsProfiler:Too many DML statements: 151 
Stack Trace: (LeadsProfiler)

Methods : 
testManyChanges
testBulkInsert

Code : 

@isTest
private class StatusChangeTest {
    static testMethod void testStatusChange() {
        Lead l = new Lead();
        l.Company = 'Hogwarts School of Witchcraft and Wizardry';
        l.FirstName = 'Harry';
        l.LastName = 'Potter';
        l.Status = 'Contacted - Qualifying';
        l.LeadSource = 'Other';
        insert l;
        
        l = [select Id, Status, Status_Change_Date__c from Lead where Id = :l.Id];
        
        System.assertEquals(l.Status_Change_Date__c, Date.today());
        
        l.Status_Change_Date__c = Date.today().addDays(-3);
        update l;
                
        Test.startTest();
        SchedulableContext sc;
        new StatusChange().execute(sc);
        Test.stopTest();
        
        l = [select Id, Status from Lead where Id = :l.Id];
        
        System.assertEquals(l.Status, 'Not Contacted');
    }
    
    static testMethod void testManyChanges(){
        List<Lead> leads = new List<Lead>();
        for(Integer i =0; i < 201; i++){
            Lead l = new Lead();
            l.Company = 'Hogwarts School of Witchcraft and Wizardry';
            l.FirstName = 'Harry' + i;
            l.LastName = 'Potter';
            l.Status = 'Contacted - Qualifying';
            l.LeadSource = 'Other';
            
            leads.add(l);
        }
        
        insert leads;
        
        leads = [select LastName, Status, Status_Change_Date__c from Lead where LastName = 'Potter'];
        
        for(Lead l: leads){
            l.Status_Change_Date__c = Date.today().addDays(-3);
        }
        update leads;
        
        
        Test.startTest();
        SchedulableContext sc;
        new StatusChange().execute(sc);
        Test.stopTest();
        
        leads = [select Id, Status from Lead where LastName = 'Potter'];
        
        for(Lead l: leads){
            System.assertEquals(l.Status, 'Not Contacted');
        }
    }
}



public class UpdateAlertStatusOnAccountTest {
    static testMethod void testInsertWithDuplicateAccounts() {
        // create one account
        Account testAccount = new Account(Name='UpdateAlertStatusOnAccountTest');
        insert testAccount;
        
        // create two alerts on the same account
        Alert_Notes__c alert1 = new Alert_Notes__c(Account__c = testAccount.Id, Impacts__c = 'Account', Alert_Status__c = 'Red');
        Alert_Notes__c alert2 = new Alert_Notes__c(Account__c = testAccount.Id, Impacts__c = 'Account', Alert_Status__c = 'Green');
        
        insert new Alert_Notes__c[] { alert1, alert2 };
        
        // make sure the second one takes.
        
        testAccount = [select Id, Alert_Status__c from Account where Id = :testAccount.Id];
        System.assertEquals('Green', testAccount.Alert_Status__c);
    }
    
    static testMethod void testImpacts() {
        Account testAccount = new Account(Name='UpdateAlertStatusOnAccountTest');
        insert testAccount;
        
        Contact c1 = new Contact(FirstName = 'Test', LastName='UpdateAlertStatus', AccountId = testAccount.Id);
        insert c1;
        
        // create two alerts on the same account
        Alert_Notes__c alert1 = new Alert_Notes__c(Account__c = testAccount.Id, Contact__c = c1.Id, Impacts__c = 'Contact', Alert_Status__c = 'Red');
        Alert_Notes__c alert2 = new Alert_Notes__c(Account__c = testAccount.Id, Contact__c = c1.Id, Impacts__c = 'Account + Contact', 
                                                   Alert_Status__c = 'Green');
        Alert_Notes__c alert3 = new Alert_Notes__c(Account__c = testAccount.Id, Impacts__c = 'Contact', 
                                                   Alert_Status__c = 'Green');
        
        insert alert1;
        
        testAccount = [select Id, Alert_Status__c from Account where Id = :testAccount.Id];
        c1 = [select Id, Alert_Status__c from Contact where Id = :c1.Id];
        
        System.assertEquals(null, testAccount.Alert_Status__c);
        System.assertEquals('Red', c1.Alert_Status__c);
        
        insert alert2;
        
        testAccount = [select Id, Alert_Status__c from Account where Id = :testAccount.Id];
        c1 = [select Id, Alert_Status__c from Contact where Id = :c1.Id];
        
        System.assertEquals('Green', testAccount.Alert_Status__c);
        System.assertEquals('Green', c1.Alert_Status__c);
        
        Boolean error = false;
        try {
            insert alert3;
        } catch(DMLException e) {
            error = true;
        }
        
        System.assert(error);
    }
    
    static testMethod void testBulkInsert() {
        // create 201 accounts and an alert note per account -- provided 
        String[] alertStats = new String[] { 'Red', 'Yellow', 'Green'};
        
        List<Account> accountList = new List<Account>();
        
        for(Integer i = 0; i < 201; i++) {
            if(accountList.size() == 200) {
                insert accountList;
                accountList.clear();
            }
            
            Account a = new Account(Name = 'UpdateAlertStatusOnAccountTest' + i);
            accountList.add(a);
        }
        
        insert accountList;
        
        // re-select the accounts
        accountList = [select Id from Account where Name like '%UpdateAlertStatusOnAccountTest%'];
        
        List<Alert_Notes__c> alertNotes = new List<Alert_Notes__c>();
        
        Test.startTest();
        
        for(Integer i = 0; i < 201; i++) {
            if(alertNotes.size() == 200) {
                insert alertNotes;
                alertNotes.clear();
            }
            
            alertNotes.add(new Alert_Notes__c(Account__c = accountList[i].Id, Impacts__c = 'Account', Alert_Status__c = alertStats[Math.mod(i, 3)]));
        }
        
        insert alertNotes;
        

        
        // there should be 67 of each color.
        Integer redCount = [select count() from Account where Alert_Status__c = 'Red' and Name like '%UpdateAlertStatusOnAccountTest%'];
        Integer yellowCount = [select count() from Account where Alert_Status__c = 'Yellow' and Name like '%UpdateAlertStatusOnAccountTest%'];
        Integer greenCount = [select count() from Account where Alert_Status__c = 'Green' and Name like '%UpdateAlertStatusOnAccountTest%'];
        
        System.assertEquals(67, redCount);
        System.assertEquals(67, yellowCount);
        System.assertEquals(67, greenCount);
        
        Test.stopTest();
    }
}


Please let me know what could I do to solve this.
sandeep sankhlasandeep sankhla
Hi Ankit,

Can you share below class code here
LeadsProfiler:Too 

Thanks
Sandeep
Ankit Singh 6Ankit Singh 6
Hi Sandeep

I do not find LeadProfiler code(neither in class nor in trigger) in my system. Please let me know if ytou need any thing else 
sandeep sankhlasandeep sankhla
Hi Ankit,

Can you share the screen shot of the error when you are getting..so I can look into that and help you out..

Thanks
Sandeep
Ankit Singh 6Ankit Singh 6
Hi

PFAUser-added image
Ankit Singh 6Ankit Singh 6
Hi

I am still facing above issue. Please let me know any solution for this.
Ankit Singh 6Ankit Singh 6
StatusChange
global class StatusChange implements Schedulable{
    global void execute(SchedulableContext SC) {
        List<Lead> leads = [select Id, isConverted, Status, Status_Change_Date__c from Lead where Status = 'Contacted - Qualifying' AND Status_Change_Date__c < :Date.today().addDays(-2) AND isConverted = FALSE];
        for(Lead l : leads){
            l.Status = 'Not Contacted';
        }
        update leads;
    }
}

UpdateAlertStatus on Alert_Notes__c 
trigger UpdateAlertStatus on Alert_Notes__c (before insert) {
    Map<ID, Account> accountsToUpdate = new Map<ID, Account>();
    Map<ID, Contact> contactsToUpdate = new Map<ID, Contact>();
    
    for(Alert_Notes__c note : Trigger.new) {
        Boolean updateContact = false;
        Boolean updateAccount = false;
        
        if(note.Impacts__c == 'Account') {
            updateAccount = true; 
        } else if(note.Impacts__c == 'Contact') {
            updateContact = true;
        } else if(note.Impacts__c == 'Account + Contact') {
            updateAccount = true;
            updateContact = true;
        }
        
        if(updateAccount) {
            Account toUpdate = accountsToUpdate.get(note.Account__c);
            if(toUpdate == null) {
                toUpdate = new Account(Id = note.Account__c);
                accountsToUpdate.put(toUpdate.Id, toUpdate);
            }
            String alertStatus = note.Alert_Status__c;
            toUpdate.Alert_Status__c = alertStatus;
            
            if(alertStatus == 'Red' || alertStatus == 'Yellow' || alertStatus == 'Green') {
                toUpdate.Alert_Note__c = note.Alert_Notes__c;
            }
        }
        
        if(updateContact) {
            if(note.Contact__c != null) {
                Contact contactToUpdate = contactsToUpdate.get(note.Contact__c);
                if(contactToUpdate == null) {
                    contactToUpdate = new Contact(Id = note.Contact__c);
                    contactsToUpdate.put(contactToUpdate.Id, contactToUpdate);
                }
            
                contactToUpdate.Alert_Status__c = note.Alert_Status__c;
            } else {
                note.Contact__c.addError('You must specify a contact when alert note impacts contact');
            }
        }
    }
    
    // update whatever is left in queue.
    update accountsToUpdate.values();
    update contactsToUpdate.values();
}