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
Roger WickiRoger Wicki 

static counter variable resets

Hi Community

So I've run into a strange behaviour for a static counter variable in one of my classes. I declare the variable in the beginning of the class:

public class NZZOutputFileGenerator
{
	// ...
	private static Integer lineCounter;
	//...
Then, in a static constructor I assign it its value (I have already tried directly assigning the value upon declaration, nothing changed)
    // ...
    static {
		lineCounter = 1;
		//...
	}
A public method allows the scheduled class to execute the classes function via the following function:
    public void handle() {
		prepareData();
		createFile();
		sendFile();
	}
In the createFile method, I loop over a collection of Data retrieved by the previous method and perform 3 further actions (calling methods, in which I increase the lineCounter)
    private void createFile() {
		for ( Installment__c installment : installmentMap.values() ) {
			fileContent += fileContent.equals('') ? getFileHead(installment.OpportunityId__r.AccountId, installment.OpportunityId__c) :
				fileContent + getFileHead(installment.OpportunityId__r.AccountId, installment.OpportunityId__c);
			fileContent += getFileHeader(installment.OpportunityId__r.AccountId, installment.OpportunityId__c, installment.Id);
			fileContent += getFilePositions(installmentToInstallmentPositionMap.get(installment.Id));
		}
	}
Every of those function contains the following part:
lineItems.add(String.valueOf(lineCounter++));
These are the only occurances of lineCounter in my class.

Now what I wanted is that for every Installment__c in the looped over collection the lineCounter continually increases. However, this is not the case. For every Installment__c in the createFile() method, the lineCounter resets to 1. I have no clue why it does that... a static variable should not change during a session unless explicitly ordered to do so...

This happened to me during testing my code in my sandbox using the ExecuteAnonymous function in the SF Dev Console. Maybe this has an influence...

I appreciate any insights.
Best Answer chosen by Roger Wicki
Roger WickiRoger Wicki
Hey there

I've just found, that the error was in a totally different place... I continued to process those texts in the three methods within the loop as single lines of a bigger file. Because of the follwoing statement, with every Installment, the text got resubmitted (kind of) which was not the goal. Thus it looked like the counter did not work as intended. But in the end I was just stupid ;)
fileContent += fileContent.equals('') ? getFileHead(installment.OpportunityId__r.AccountId, installment.OpportunityId__c) :
4
                fileContent + getFileHead(installment.OpportunityId__r.AccountId, installment.OpportunityId__c);
fileContent = ... would have been the right approach.

Sorry for the fuzz people ;)
 

All Answers

Sagar PareekSagar Pareek
Can you please post whole code here? I am not sure where you put the counter increament in for loop.
I would suggest to put System.debug statement in each method and for loop  and then check debug logs to observe the behaviour. 
Roger WickiRoger Wicki
Hey there

I've just found, that the error was in a totally different place... I continued to process those texts in the three methods within the loop as single lines of a bigger file. Because of the follwoing statement, with every Installment, the text got resubmitted (kind of) which was not the goal. Thus it looked like the counter did not work as intended. But in the end I was just stupid ;)
fileContent += fileContent.equals('') ? getFileHead(installment.OpportunityId__r.AccountId, installment.OpportunityId__c) :
4
                fileContent + getFileHead(installment.OpportunityId__r.AccountId, installment.OpportunityId__c);
fileContent = ... would have been the right approach.

Sorry for the fuzz people ;)
 
This was selected as the best answer