[SalesForce] Static Boolean Property Getting set to ‘TRUE’ by default

Well, they say that no platform is perfect, and Salesforce definitely has its share of quirks. Here's one more.
I have the following structure:

Trigger on Lead:

//some random code
system.debug(myClass.myBooleanVar);
if(!myClass.myBooleanVar){
  //some code
}
//some random code

in the myClass Class:

public static boolean myBooleanVar = FALSE;

As you all would know, this is the standard way to avoid recursion in triggers.

Scenario 1:
If I upload 100 records (say) and get at least 1 error, the 'system.debug' statement is printing 'TRUE'. This doesn't make sense as I am explicitly initialising the boolean to 'FALSE' in 'myClass'.

Scenario 2:
If I upload 100 records and if all are successful, the 'system.debug' statement is printing 'FALSE'. (as desired)

Any ideas on what the issue might be? Thanks!

Best Answer

Your code with line "myBooleanVar = value;" is creating problem for you. If Salesforce debug log is too large then it can skip some part and don't show in debug log. Try to replce the line "myBooleanVar = value;" with "myBooleanVar = False;" and perform the DML to run the trigger again. Now check the debug log to find out True and if you don't it then am correct, So findout from where that variable is set to True.

Related Topic