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
bdardinbdardin 

Contents of Arrays Overwritten

I am attempting to build a Visualforce page with pagination using this guide. However when I put it in place, it keeps putting the same lines into each array - even though debug statements show that it's creating the arrays and adding them to a list of arrays successfully. Somehow the content of each array is getting overwritten, but I have no idea what's causing it.

 

I am not including the entire controller - just the code that I have been testing through the developer console.

 

static Integer FIRST_BREAK = 10;
static Integer SUBSEQ_BREAKS = 30;

ID woID = 'a0wE0000000NTi0';

RecordType rt = [SELECT Id FROM RecordType WHERE Name = 'Products Serviced' AND SobjectType = 'SVMXC__Service_Order_Line__c'];
        
        List<SVMXC__Service_Order_Line__c> lines = [SELECT Id, Building__c, Position__c, Product_of_SN__c, SN__c, SVMXC__Serial_Number__r.Condition__c FROM SVMXC__Service_Order_Line__c WHERE SVMXC__Service_Order__c = :woID AND RecordTypeId = :rt.Id ORDER BY Position__c ASC NULLS LAST];
        
        List<SVMXC__Service_Order_Line__c[]> pageLines = new List<SVMXC__Service_Order_Line__c[]>();
        SVMXC__Service_Order_Line__c[] pageOfLines = new SVMXC__Service_Order_Line__c[]{};
        
        Integer counter = 0;
        Boolean firstBreakFound = false;
        Boolean setSubSeqBreak = false;  
        Integer breakPoint = FIRST_BREAK;
        
        for(SVMXC__Service_Order_Line__c line : lines) {
            system.debug('Counter: ' + counter + ' IP: ' + line.SN__c);
            if(counter < breakPoint) {
                pageOfLines.add(line);
                counter++;
            }
            
            if(counter == breakPoint) {
                if(!firstBreakFound) {
                    firstBreakFound = true;
                    setSubSeqBreak = true;
                }
                
                counter = 0;
                pageLines.add(pageOfLines);
                system.debug('pageOfLines Size: ' + pageOfLines.size());
                system.debug('pageLines Size: ' + pageLines.size());
                pageOfLines.clear();
            }
            
            if(setSubSeqBreak) {
                breakPoint = SUBSEQ_BREAKS;
                setSubSeqBreak = false;
            }
        }
        
	if(!pageOfLines.isEmpty()) {
            pageLines.add(pageOfLines);
        }

system.debug('Size After Adding Leftover: ' + pageLines.size());

Integer counter2 = 0;
for(SVMXC__Service_Order_Line__c[] lineArray : pageLines) {
    system.debug('Size of Array #' + counter2 + ' : ' + lineArray.size());
    
    for(SVMXC__Service_Order_Line__c li : lineArray) {
    	system.debug('Array #: ' + counter2 + ' IP: ' + li.SN__c);
    }
    
    counter2++;
}

 And here is the debug log, showing that after the for loop is completed, the size and contents of the arrays have suddenly become the same:

 

11:20:59.235 (235006000)|USER_DEBUG|[45]|DEBUG|Counter: 0 IP: MCK3170
11:20:59.235 (235163000)|USER_DEBUG|[45]|DEBUG|Counter: 1 IP: 329120
11:20:59.235 (235277000)|USER_DEBUG|[45]|DEBUG|Counter: 2 IP: MCK3179
11:20:59.235 (235387000)|USER_DEBUG|[45]|DEBUG|Counter: 3 IP: 329263
11:20:59.235 (235495000)|USER_DEBUG|[45]|DEBUG|Counter: 4 IP: MCK3180
11:20:59.235 (235602000)|USER_DEBUG|[45]|DEBUG|Counter: 5 IP: 329239
11:20:59.235 (235709000)|USER_DEBUG|[45]|DEBUG|Counter: 6 IP: MCK3181
11:20:59.235 (235815000)|USER_DEBUG|[45]|DEBUG|Counter: 7 IP: 329132
11:20:59.235 (235921000)|USER_DEBUG|[45]|DEBUG|Counter: 8 IP: MCK3182
11:20:59.236 (236033000)|USER_DEBUG|[45]|DEBUG|Counter: 9 IP: 329109
11:20:59.236 (236173000)|USER_DEBUG|[59]|DEBUG|pageOfLines Size: 10
11:20:59.236 (236239000)|USER_DEBUG|[60]|DEBUG|pageLines Size: 1
11:20:59.236 (236361000)|USER_DEBUG|[45]|DEBUG|Counter: 0 IP: MCK3183
11:20:59.236 (236471000)|USER_DEBUG|[45]|DEBUG|Counter: 1 IP: MCK3184
11:20:59.236 (236579000)|USER_DEBUG|[45]|DEBUG|Counter: 2 IP: 329183
11:20:59.236 (236685000)|USER_DEBUG|[45]|DEBUG|Counter: 3 IP: MCK3185
11:20:59.236 (236790000)|USER_DEBUG|[45]|DEBUG|Counter: 4 IP: 329203
11:20:59.236 (236897000)|USER_DEBUG|[45]|DEBUG|Counter: 5 IP: MCK3186
11:20:59.237 (237003000)|USER_DEBUG|[45]|DEBUG|Counter: 6 IP: 329150
11:20:59.237 (237117000)|USER_DEBUG|[45]|DEBUG|Counter: 7 IP: MCK3187
11:20:59.237 (237228000)|USER_DEBUG|[45]|DEBUG|Counter: 8 IP: 329060
11:20:59.237 (237339000)|USER_DEBUG|[45]|DEBUG|Counter: 9 IP: MCK3188
11:20:59.237 (237446000)|USER_DEBUG|[45]|DEBUG|Counter: 10 IP: 329195
11:20:59.237 (237553000)|USER_DEBUG|[45]|DEBUG|Counter: 11 IP: MCK3171
11:20:59.237 (237659000)|USER_DEBUG|[45]|DEBUG|Counter: 12 IP: 329168
11:20:59.237 (237765000)|USER_DEBUG|[45]|DEBUG|Counter: 13 IP: MCK3189
11:20:59.237 (237871000)|USER_DEBUG|[45]|DEBUG|Counter: 14 IP: 329098
11:20:59.237 (237977000)|USER_DEBUG|[45]|DEBUG|Counter: 15 IP: MCK3190
11:20:59.238 (238090000)|USER_DEBUG|[45]|DEBUG|Counter: 16 IP: 329173
11:20:59.238 (238199000)|USER_DEBUG|[45]|DEBUG|Counter: 17 IP: MCK3191
11:20:59.238 (238308000)|USER_DEBUG|[45]|DEBUG|Counter: 18 IP: 329253
11:20:59.238 (238416000)|USER_DEBUG|[45]|DEBUG|Counter: 19 IP: MCK3192
11:20:59.238 (238522000)|USER_DEBUG|[45]|DEBUG|Counter: 20 IP: 329208
11:20:59.238 (238628000)|USER_DEBUG|[45]|DEBUG|Counter: 21 IP: MCK3193
11:20:59.238 (238733000)|USER_DEBUG|[45]|DEBUG|Counter: 22 IP: 329192
11:20:59.238 (238839000)|USER_DEBUG|[45]|DEBUG|Counter: 23 IP: MCK3194
11:20:59.238 (238945000)|USER_DEBUG|[45]|DEBUG|Counter: 24 IP: 329159
11:20:59.239 (239057000)|USER_DEBUG|[45]|DEBUG|Counter: 25 IP: MCK3195
11:20:59.239 (239165000)|USER_DEBUG|[45]|DEBUG|Counter: 26 IP: 329259
11:20:59.239 (239274000)|USER_DEBUG|[45]|DEBUG|Counter: 27 IP: MCK3172
11:20:59.239 (239384000)|USER_DEBUG|[45]|DEBUG|Counter: 28 IP: 329232
11:20:59.239 (239490000)|USER_DEBUG|[45]|DEBUG|Counter: 29 IP: MCK3173
11:20:59.239 (239584000)|USER_DEBUG|[59]|DEBUG|pageOfLines Size: 30
11:20:59.239 (239623000)|USER_DEBUG|[60]|DEBUG|pageLines Size: 2
11:20:59.239 (239716000)|USER_DEBUG|[45]|DEBUG|Counter: 0 IP: 329245
11:20:59.239 (239823000)|USER_DEBUG|[45]|DEBUG|Counter: 1 IP: MCK3174
11:20:59.239 (239930000)|USER_DEBUG|[45]|DEBUG|Counter: 2 IP: 329234
11:20:59.240 (240042000)|USER_DEBUG|[45]|DEBUG|Counter: 3 IP: MCK3175
11:20:59.240 (240150000)|USER_DEBUG|[45]|DEBUG|Counter: 4 IP: 329223
11:20:59.240 (240259000)|USER_DEBUG|[45]|DEBUG|Counter: 5 IP: MCK3176
11:20:59.240 (240369000)|USER_DEBUG|[45]|DEBUG|Counter: 6 IP: 329213
11:20:59.240 (240475000)|USER_DEBUG|[45]|DEBUG|Counter: 7 IP: MCK3177
11:20:59.240 (240581000)|USER_DEBUG|[45]|DEBUG|Counter: 8 IP: 329219
11:20:59.240 (240687000)|USER_DEBUG|[45]|DEBUG|Counter: 9 IP: MCK3178
11:20:59.240 (240793000)|USER_DEBUG|[45]|DEBUG|Counter: 10 IP: 329137
11:20:59.240 (240979000)|USER_DEBUG|[74]|DEBUG|Size After Adding Leftover: 3
11:20:59.241 (241247000)|USER_DEBUG|[78]|DEBUG|Size of Array #0 : 11
11:20:59.241 (241354000)|USER_DEBUG|[81]|DEBUG|Array #: 0 IP: 329245
11:20:59.241 (241411000)|USER_DEBUG|[81]|DEBUG|Array #: 0 IP: MCK3174
11:20:59.241 (241464000)|USER_DEBUG|[81]|DEBUG|Array #: 0 IP: 329234
11:20:59.241 (241516000)|USER_DEBUG|[81]|DEBUG|Array #: 0 IP: MCK3175
11:20:59.241 (241568000)|USER_DEBUG|[81]|DEBUG|Array #: 0 IP: 329223
11:20:59.241 (241620000)|USER_DEBUG|[81]|DEBUG|Array #: 0 IP: MCK3176
11:20:59.241 (241673000)|USER_DEBUG|[81]|DEBUG|Array #: 0 IP: 329213
11:20:59.241 (241725000)|USER_DEBUG|[81]|DEBUG|Array #: 0 IP: MCK3177
11:20:59.241 (241777000)|USER_DEBUG|[81]|DEBUG|Array #: 0 IP: 329219
11:20:59.241 (241829000)|USER_DEBUG|[81]|DEBUG|Array #: 0 IP: MCK3178
11:20:59.241 (241881000)|USER_DEBUG|[81]|DEBUG|Array #: 0 IP: 329137
11:20:59.241 (241973000)|USER_DEBUG|[78]|DEBUG|Size of Array #1 : 11
11:20:59.242 (242048000)|USER_DEBUG|[81]|DEBUG|Array #: 1 IP: 329245
11:20:59.242 (242103000)|USER_DEBUG|[81]|DEBUG|Array #: 1 IP: MCK3174
11:20:59.242 (242155000)|USER_DEBUG|[81]|DEBUG|Array #: 1 IP: 329234
11:20:59.242 (242207000)|USER_DEBUG|[81]|DEBUG|Array #: 1 IP: MCK3175
11:20:59.242 (242264000)|USER_DEBUG|[81]|DEBUG|Array #: 1 IP: 329223
11:20:59.242 (242319000)|USER_DEBUG|[81]|DEBUG|Array #: 1 IP: MCK3176
11:20:59.242 (242372000)|USER_DEBUG|[81]|DEBUG|Array #: 1 IP: 329213
11:20:59.242 (242424000)|USER_DEBUG|[81]|DEBUG|Array #: 1 IP: MCK3177
11:20:59.242 (242476000)|USER_DEBUG|[81]|DEBUG|Array #: 1 IP: 329219
11:20:59.242 (242528000)|USER_DEBUG|[81]|DEBUG|Array #: 1 IP: MCK3178
11:20:59.242 (242580000)|USER_DEBUG|[81]|DEBUG|Array #: 1 IP: 329137
11:20:59.242 (242671000)|USER_DEBUG|[78]|DEBUG|Size of Array #2 : 11
11:20:59.242 (242738000)|USER_DEBUG|[81]|DEBUG|Array #: 2 IP: 329245
11:20:59.242 (242790000)|USER_DEBUG|[81]|DEBUG|Array #: 2 IP: MCK3174
11:20:59.242 (242842000)|USER_DEBUG|[81]|DEBUG|Array #: 2 IP: 329234
11:20:59.242 (242894000)|USER_DEBUG|[81]|DEBUG|Array #: 2 IP: MCK3175
11:20:59.242 (242946000)|USER_DEBUG|[81]|DEBUG|Array #: 2 IP: 329223
11:20:59.242 (242997000)|USER_DEBUG|[81]|DEBUG|Array #: 2 IP: MCK3176
11:20:59.243 (243062000)|USER_DEBUG|[81]|DEBUG|Array #: 2 IP: 329213
11:20:59.243 (243115000)|USER_DEBUG|[81]|DEBUG|Array #: 2 IP: MCK3177
11:20:59.243 (243168000)|USER_DEBUG|[81]|DEBUG|Array #: 2 IP: 329219
11:20:59.243 (243220000)|USER_DEBUG|[81]|DEBUG|Array #: 2 IP: MCK3178
11:20:59.243 (243275000)|USER_DEBUG|[81]|DEBUG|Array #: 2 IP: 329137

 Anyone have any idea what I'm doing wrong here?? I would greatly appreciate any input!