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.
{"cc_re__cc_re__cc_by_sa_3_0": ["sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com?utm_source=stackoverflow.com&amp;utm_medium=site-ui&amp;utm_campaign=footerlink\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2014 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2014.9.17.1862\r\n            </div>\r\n                <div id=\"additional-notices\">\r\n                    Salesforce is a registered trademark of salesforce.com, Inc.\r\n                </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warnin"], "copyright__copyright__copyright": ["age syntax.\n\nWhen 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.\n\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}