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
Akshay_AAkshay_A 

Not able to access class variable in trigger

Hello,

I want to insert a value in my case field on before insert trigger . I have a static variable in controller whose value Iam initializing in a method in controller and when I access that variable in trigger It is showing "NULL".
and if I initialize the value while declaring then trigger prints the value.
refer the code below:

//this is the class.
public with sharing class Demo{
  public static String test;

public void testMethod(){

   test  = 'hello';


}

//this is the trigger.

trigger TestTrigger on Case (before insert) {
   
    System.debug(Demo.test);  //prints null

}
Best Answer chosen by Akshay_A
GulshanRajGulshanRaj
Hi Akshay,

It look like you are not creating case record just after calling action function. Please note static variable keep its state for single execution context. If your trigger and action function are in different execution context then values of static variable will not retain its state and get different (reset) value.

I have build a super simple VF page, I hope this will help you.
This is how my page look like:
User-added image
Your Demo Class:
public with sharing class Demo{
  public static String test;

    public void assignTest(){
    
       test  = 'hello';
    } 

}


VF page:
<apex:page controller="testActionController" >
<apex:form >
    <apex:actionFunction name="CallFunction" action="{!changTestValue}"/>
    <button onclick="return CallFunction();" >Fire </button>
</apex:form>
</apex:page>

Controller:
public class testActionController {
    public void changTestValue()
    {
        Demo objDemo= new Demo();
        objDemo.assignTest();
        case myCase = new case();
        mycase.origin='phone';
        insert mycase;
    }
}

On apply system.debug inside case trigger, I am getting static ​variable correct values.


Please let me know if you have any question


Thanks
Gulshan Raj
 

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Akshay Aggarwal,

May I suggest you please refer the below link for reference to access a class variable in a trigger. Thanks
Rahul Kumar
GulshanRajGulshanRaj
Hi Akshay,

I think somewhere you are not calling your Demo class method before case get trigger. Please try with call your method first which will set value of your static variable and available inside trigger.

Or share your complete code.

Thanks
Gulshan Raj 
Shruti SShruti S
You have initialised the test variable in the testMethod, but unfortunately you have not invoked the method anywhere in the trigger. Only if you call the method anywhere, the values in it would get initialised. You can access the static variables only with the help of the class name and not the object variable.
Here is how the corrected code of the trigger should look like - 
trigger TestTrigger on Case ( before insert ) {
    Demo newDemo = new Demo();
    newDemo.testMethod();
    System.debug( Demo.test );
}
Also, it is advised to change the method name defined in the class from 'testMethod' to some other name as 'testMethod' is a keyword.
 
Akshay_AAkshay_A
Iam calling the method through an action function on visualforce page and it is able to update value of static variable . The trigger is called after the call to action function . So, I think the trigger must get the updated value but it is not.
GulshanRajGulshanRaj
Hi Akshay,

It look like you are not creating case record just after calling action function. Please note static variable keep its state for single execution context. If your trigger and action function are in different execution context then values of static variable will not retain its state and get different (reset) value.

I have build a super simple VF page, I hope this will help you.
This is how my page look like:
User-added image
Your Demo Class:
public with sharing class Demo{
  public static String test;

    public void assignTest(){
    
       test  = 'hello';
    } 

}


VF page:
<apex:page controller="testActionController" >
<apex:form >
    <apex:actionFunction name="CallFunction" action="{!changTestValue}"/>
    <button onclick="return CallFunction();" >Fire </button>
</apex:form>
</apex:page>

Controller:
public class testActionController {
    public void changTestValue()
    {
        Demo objDemo= new Demo();
        objDemo.assignTest();
        case myCase = new case();
        mycase.origin='phone';
        insert mycase;
    }
}

On apply system.debug inside case trigger, I am getting static ​variable correct values.


Please let me know if you have any question


Thanks
Gulshan Raj
 
This was selected as the best answer
GulshanRajGulshanRaj
Hi @Akshay_A,

Was that worked? Please let me know if you have any question.
Akshay_AAkshay_A
Hi @GulshanRaj,
Thanks It is working fine but I have a different scenario . 

I have a visualforce page and through that visualforce page I am updating the value of static variable.

And after that I am creating the case using this page:
User-added image

In this scenario I am not getting the value in trigger.
Is this the issue of execution context?
 
GulshanRajGulshanRaj
Hi @Akshay_A,

To understand execution context I am sharing my notes created 4-5 years back. Please have a look to understand how it works.

User-added image

Thanks
Gulshan Raj