You need to sign in to do that
Don't have an account?
Akshay_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
}
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
}
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:
Your Demo Class:
VF page:
Controller:
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
May I suggest you please refer the below link for reference to access a class variable in a trigger.
- https://stackoverflow.com/questions/7437549/how-do-i-update-a-class-variable-from-a-trigger-in-apex
ThanksRahul Kumar
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
Here is how the corrected code of the trigger should look like - Also, it is advised to change the method name defined in the class from 'testMethod' to some other name as 'testMethod' is a keyword.
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:
Your Demo Class:
VF page:
Controller:
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
Was that worked? Please let me know if you have any question.
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:
In this scenario I am not getting the value in trigger.
Is this the issue of execution context?
To understand execution context I am sharing my notes created 4-5 years back. Please have a look to understand how it works.
Thanks
Gulshan Raj