Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

It says here that trigger context variables are contained in the System.Trigger class.

However, such a class is unknown to the Apex language reference, and Trigger doesn't seem to behave like a class either. For example, the expected property Trigger.class is inaccessible from Apex code.

Trigger context contents and usage is reasonably clear, but what exactly is Trigger?

Background: The idea of using a test stub in place of a real DML-fired trigger context came up while writing tests for a trigger handler class. I would first need to create a stub trigger context, and then execute my trigger in it. I'm yet to find a way to do that.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

System.Trigger behaves as a final static variable of an unknown, or anonymous, class, that resides in the System namespace, which itself behaves much like a static final variable of an anonymous class. Namespaces work like they do in C#, in that they can contain both members and classes, but without an explicit Type that can be referenced, as a class would be referenced.

As a more direct answer to your question, System.Trigger can only be accessed in a trigger context, as it is always null otherwise, and furthermore has a private setter, so writing a test method requires constructing the necessary records to call a DML operation on. You can't fake trigger context. At least, not with any method I've tried.

Unfortunately, the language has very little reflection available, and isn't open source, so the best you can get from an outside developer like myself is a reasonably vague answer.

I would speculate that many standard features of the language are written using special exceptions that would make them impossible to replicate in the current language syntax.

When practical, write your methods to accept plain lists or maps, and use trigger variables from triggers, and custom lists or maps in test methods. This delegates trigger syntax to only triggers, and test methods can operate independently of the trigger variables.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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