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
Shawn Reichner 29Shawn Reichner 29 

Visualforce Error! Please Help...Modified rows exist in the records collection!

Hello awesome Devs!

I have the following Vf Page, Extension Controller and Test Class that I am attempting to get into Production.  The VF page and controller acts as it should and does the job awesomly, however I get the following error when attempting to run the test class....How can I get past this and resolve?

Thank you all in advance for any help you can provide,

Shawn


Extension Class -
 
public with sharing class ABMDashboardController {

    public List<Account> acc{get;set;}
    public List<Account> acc2{get;set;}
    public List<Account> acc3{get;set;}
    public List<Account> acc4{get;set;}
    public List<Account> acc5{get;set;}
    public List<Account> acc6{get;set;}
    public List<Account> acc7{get;set;}
    public List<Account> acc8{get;set;}
    public List<Account> acc9{get;set;}
    public List<Account> acc10{get;set;}
    public List<Account> acc11{get;set;}
    public List<Account> acc12{get;set;}
    public List<Account> acc13{get;set;}
    public List<Account> acc14{get;set;}
    public List<Account> acc15{get;set;}
    public List<Account> acc16{get;set;}
    public List<Account> acc17{get;set;}
    public List<Account> acc18{get;set;}
    public List<Account> acc19{get;set;}
    public List<Account> acc20{get;set;}
    public List<Account> acc21{get;set;}
    public List<Account> acc22{get;set;}
    public List<Account> acc23{get;set;}
    public List<Account> acc24{get;set;}
    public List<Account> acc25{get;set;}
    public List<Account> acc26{get;set;}
    public List<Account> acc27{get;set;}
    public List<Account> acc28{get;set;}
    public List<Account> acc29{get;set;}
    public List<Account> acc30{get;set;}
    public List<Account> acc31{get;set;}
    public List<Account> acc32{get;set;}
    public List<Account> acc33{get;set;}
    public List<Account> acc34{get;set;}
    public List<Account> acc35{get;set;}
    public List<Account> acc36{get;set;}
    public List<Account> acc37{get;set;}
    public List<Account> acc38{get;set;}
    
    public ABMDashboardController(APexPages.StandardSetController controller1)
    {
        controller1.setPageSize(40);
        acc = new List<Account>();
        acc = [SELECT Id, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Northwest' AND Status__c = 'Active'];
        acc2 = new List<Account>();
        acc2 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Northwest' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc3 = new List<Account>();
        acc3 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Southwest' AND Status__c = 'Active'];
        acc4 = new List<Account>();
        acc4 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Southwest' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc5 = new List<Account>();
        acc5 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Upper South Central' AND Status__c = 'Active'];
        acc6 = new List<Account>();
        acc6 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Upper South Central' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc7 = new List<Account>();
        acc7 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Lower South Central' AND Status__c = 'Active'];
        acc8 = new List<Account>();
        acc8 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Lower South Central' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc9 = new List<Account>();
        acc9 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Upper North Central' AND Status__c = 'Active'];
        acc10 = new List<Account>();
        acc10 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Upper North Central' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc11 = new List<Account>();
        acc11 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Lower North Central' AND Status__c = 'Active'];
        acc12 = new List<Account>();
        acc12 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Lower North Central' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc13 = new List<Account>();
        acc13 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Mid Atlantic' AND Status__c = 'Active'];
        acc14 = new List<Account>();
        acc14 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Mid Atlantic' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc15 = new List<Account>();
        acc15 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Southeast' AND Status__c = 'Active'];
        acc16 = new List<Account>();
        acc16 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Southeast' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc17 = new List<Account>();
        acc17 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Florida' AND Status__c = 'Active'];
        acc18 = new List<Account>();
        acc18 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Florida' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc19 = new List<Account>();
        acc19 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'NYNJ' AND Status__c = 'Active'];
        acc20 = new List<Account>();
        acc20 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'NYNJ' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc21 = new List<Account>();
        acc21 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Northeast' AND Status__c = 'Active'];
        acc22 = new List<Account>();
        acc22 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Northeast' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc23 = new List<Account>();
        acc23 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'MIOH' AND Status__c = 'Active'];
        acc24 = new List<Account>();
        acc24 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'MIOH' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc25 = new List<Account>();
        acc25 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'HI,AK,PR' AND Status__c = 'Active'];
        acc26 = new List<Account>();
        acc26 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'HI,AK,PR' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc27 = new List<Account>();
        acc27 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Canada East' AND Status__c = 'Active'];
        acc28 = new List<Account>();
        acc28 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Canada East' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc29 = new List<Account>();
        acc29 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Canada West' AND Status__c = 'Active'];
        acc30 = new List<Account>();
        acc30 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Canada West' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc31 = new List<Account>();
        acc31 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'APAC' AND Status__c = 'Active'];
        acc32 = new List<Account>();
        acc32 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'APAC' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc33 = new List<Account>();
        acc33 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'EMEA' AND Status__c = 'Active'];
        acc34 = new List<Account>();
        acc34 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'EMEA' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc35 = new List<Account>();
        acc35 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Latin America' AND Status__c = 'Active'];
        acc36 = new List<Account>();
        acc36 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Latin America' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
        acc37 = new List<Account>();
        acc37 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'South America' AND Status__c = 'Active'];
        acc38 = new List<Account>();
        acc38 = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'South America' AND Status__c != 'Active' ORDER BY Average_TAM_Score__c DESC];
    }
    
    public PageReference save(){
        update acc;
        return null;
    }
    
    
    
}

And finally the test class, where the error is being reported when ran...
 
@isTest(SeeAllData = True)
public class ABMDashboardControllerTest {

    
    static testMethod void tm1() {
        
        Integer Score = 50;
        
        List<Account> accts = new List<Account>();
        
        Account a1 = new Account();
        a1.Name = 'Test 1';
        a1.Armor_Territory__c = 'Northwest';
        a1.Anywhere_Score__c = Score;
        a1.Complete_Score__c = Score;
        a1.Territory_Bypass__c = True;
        accts.add(a1);
        
        Account a2 = new Account();
        a2.Name = 'Test 2';
        a2.Armor_Territory__c = 'Northwest';
        a2.Anywhere_Score__c = Score;
        a2.Complete_Score__c = Score;
        a2.Territory_Bypass__c = True;
        accts.add(a2);
        insert accts;
        
       Test.startTest();
        Test.setCurrentPage(Page.ABMDashboard);
        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(accts);
        stdSetController.setSelected(accts);
        ABMDashboardController ext = new ABMDashboardController(stdSetController);
        ext.save();
        Test.stopTest();
    }
    
    
}

The error that is returned is as follows - 

Error: System.VisualforceException: Modified rows exist in the records collection!

Stack Trace: External entry point
Class.ABMDashboardControllerTest.tm1: line 32, column 1
Best Answer chosen by Shawn Reichner 29
Shawn Reichner 29Shawn Reichner 29
Alain,

Thank you again!  I found the issue...Seems like it didnt like the List that comprised of records created using the test class.  So I have to create a list and input a SOQL query to bring back all accounts and then use that list in the controller line on 32.  This fixed th eissue and caused 100 % code coverage. 

Thank you for yoru support though, greatly appreciated! 

Shawn

All Answers

Alain CabonAlain Cabon
Hi,

@isTest(SeeAllData = True): means that you are using the real data of your org and not your test cases with two accounts.

Just try by removing  (SeeAllData = True)
 
Shawn Reichner 29Shawn Reichner 29
Alain,

Thank you for the suggestion, I get the same error with the SeeAll Data removed as well. 

Do you happen to have any other ideas?

Shawn
Alain CabonAlain Cabon
1) It seems that you want to select Status__c = 'Active'
   
Did you set this value Status__c  in your test cases?

2) Did you test if the list acc is empty before the update?

public PageReference save(){
    if ( acc != null && !acc.isEmpty() ) 
        update acc;       
   }
    return null;
 }

Let me know for the next errors if that doesn't work after this new fixes.
Shawn Reichner 29Shawn Reichner 29
Alain,

Thank you again!  I found the issue...Seems like it didnt like the List that comprised of records created using the test class.  So I have to create a list and input a SOQL query to bring back all accounts and then use that list in the controller line on 32.  This fixed th eissue and caused 100 % code coverage. 

Thank you for yoru support though, greatly appreciated! 

Shawn
This was selected as the best answer