0

I have a Boolean variable in Class A. I am trying to set it's value from class B but it is not working. Below is the sample code of which I am trying.

Class A :

global without sharing class ClassA {

global static boolean boolvar{get;set;}

public static void somemethod(){

// some code
}
}

Class B :

public without sharing class ClassB {

public override void sommethod2(){

ClassA.boolvar = true;

}
}

Now, Class B is setting this variable value to true and calling another class. Let's take it Class C. Class C now calling Class A, then in Class A, boolvar is showing null even though we are setting it in Class B.

7
  • When/where are you inspecting ClassA.boolvar? (When/where do you see it is equal to null?) – Sander de Jong Apr 20 at 7:53
  • Sander, edited the question. please check – Sfdc_Spider Apr 20 at 8:03
  • Are you setting and getting the static variable in two different executions? – Sergio Alcocer Apr 20 at 8:03
  • Sergio - No, I am setting it only in Class B – Sfdc_Spider Apr 20 at 8:05
  • Could you provide the code with ClassB.sommetod2 invoking? – pincet Apr 20 at 8:06
0

The documentation for apex properties does not mention using automatic property syntax for static variables. This means you will want to have a private static variable to contain the value of boolvar.

global without sharing class ClassA {

private static boolean boolvarValue;

global static boolean boolvar{
  get {return boolvarValue;} 
  set { boolvarValue = value; } 
}

public static void somemethod(){

// some code
}
}

This makes sense as property syntax effectively creates an unseen private instance variable to manage the value of the field, and provides a getter and setter for it, however a static field could not access an instance variable, so we need to create a static variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.