+ Start a Discussion
boBNunnyboBNunny 

APEX Class invocation and access across trigger/classes

I need to do something that I'd like to get the board's input on.

 

I need to invoke a class (i.e. MyClass) and set a flag in the Before Trigger.

 

But my After trigger will need access to the same flag to see if it was set as well components in the class itself.

 

So, if I:

     MyClass oMyClass = new MyClass();

     oMyClass.bInit = true;

 

And then in the After trigger, do:

     if (oMyClass.bInit)

That won't work because it's in a different context, will it?  So how do I get the After trigger to know that the Before trigger already fired the class?

Best Answer chosen by Admin (Salesforce Developers) 
gm_sfdc_powerdegm_sfdc_powerde

If you create a static variable in MyClass, it's not accesible from MyClass's intances in Apex (unlike Java, where it's possible but a practice frowned upon).  So you will have to set it in before trigger as MyClass.blnit = true and then you can access it in your after trigger as if (MyClass.blnit).  Hope this helps!

All Answers

gm_sfdc_powerdegm_sfdc_powerde
static variables is the way to go here.   You can set a static variable on MyClass in before trigger and access it in your after trigger.
boBNunnyboBNunny

Thought so.  So, I create the variable as static, invoke it in the Before trigger, and the After trigger will know it's set?  So the After trigger has the scope of what is in the before trigger?

 

In other words:

Before Trigger:

MyClass oMyClass = new MyClass();

oMyClass.bInit = TRUE;

 

After Trigger will accept:

if (oMyClass.bInit) ...

 

gm_sfdc_powerdegm_sfdc_powerde

If you create a static variable in MyClass, it's not accesible from MyClass's intances in Apex (unlike Java, where it's possible but a practice frowned upon).  So you will have to set it in before trigger as MyClass.blnit = true and then you can access it in your after trigger as if (MyClass.blnit).  Hope this helps!

This was selected as the best answer