ApexTrigger represents executable Apex code that runs before or after specified data manipulation language (DML) events, such as inserting or deleting records. The trigger source declares the target object and event contexts. Its companion metadata records activation status, the trigger's API version, and referenced managed-package versions.
A trigger can implement business rules directly or delegate work to an ApexClass handler. The handler is a separate component: inspecting only the trigger's metadata header does not show its complete behavior or dependencies.
Key Elements
| Element | What the Element Controls |
|---|---|
fullName |
The trigger's component name. It is inherited from the metadata base type and identifies the component, not its target object. |
content |
The executable trigger definition, inherited from MetadataWithContent. It uses base64 encoding for API transport; the on-disk Apex source is not base64 text. |
apiVersion |
Required. The API version assigned to the trigger. It is separate from the Metadata API transport version and the managed-package versions in packageVersions. |
status |
Required. Allowed values are Active, Inactive, and Deleted. These mean active, present but inactive, and marked for deletion, respectively. |
packageVersions |
The installed managed-package versions referenced by the trigger. This records dependencies; it does not establish that the trigger itself belongs to a managed package. |
namespace within packageVersions
|
Required for each package-version entry. Identifies the referenced package's namespace. |
majorNumber within packageVersions
|
Required for each package-version entry. The major portion of the referenced package version. |
minorNumber within packageVersions
|
Required for each package-version entry. The minor portion of the referenced package version. |
Behavior and Constraints
Inactive stops the trigger from being active but leaves the component present. It is not deletion. Deleted marks the trigger for removal and supports deleting a trigger when a managed package is updated; it is not a temporary bypass setting.
A code-level bypass is different from metadata deactivation. For example, trigger code can check a custom setting and return before running its business logic. That requires an explicit guard in the source. A trigger can therefore have status set to Active while its business logic is bypassed under conditions defined by the code.
The target object and event contexts belong to the Apex source declaration, not separate XML elements in the companion metadata. When the source calls a handler, inspect that handler to understand the resulting business effects.
The component name must be unique, start with a letter, and contain only alphanumeric characters and underscores. It cannot contain spaces, end with an underscore, or contain two consecutive underscores.
Changes to triggers remain subject to Apex deployment and test-execution constraints. The community discussion on activation and deactivation below also describes production test coverage and the caller's Metadata API permissions as requirements for automated status changes.
Deployment and Retrieval
ApexTrigger does not support Metadata API CRUD-based calls, which are direct operations on individual metadata components. Use the retrieve/deploy lifecycle for its source and companion metadata instead. Do not treat the inherited content field as permission to create or update this type through metadata CRUD calls.
The source file has the .trigger suffix and resides in the triggers folder of the corresponding package directory. The official sample pairs MyHelloWorld.trigger with MyHelloWorld.trigger-meta.xml. The documentation has a naming inconsistency described below.
In package.xml, use the metadata type name ApexTrigger. This type supports the * wildcard in the manifest. Its wildcard support is defined for ApexTrigger, regardless of the different wildcard rules documented for its base type.
Retrieval includes executable source as well as companion metadata. A comparison limited to status, apiVersion, and packageVersions misses code changes and source-level dependencies. Base64 is an API transport encoding for content, not the storage format of the retrieved Apex source file.
Deployment scope depends on actual source references and what already exists in the destination. A trigger can reference classes, flows, objects, fields, pages, labels, or named credentials, but the possible relationship categories do not mean every trigger needs all of them. Delegating to a class also makes that handler relevant to dependency and impact analysis.
To deactivate a trigger, change the companion metadata's status to Inactive and deploy the change. For removal, distinguish marking the trigger Deleted from merely deactivating it. Community reports also describe explicit destructive deployments for trigger removal; those reports are summarized below.
Relationships
Outgoing dependencies are embedded in executable source. The relationship label filedata represents those code-level references; it is not an ApexTrigger XML element. An incoming subscriber configuration can also affect impact analysis even though it is absent from the trigger's own metadata header.
References
-
ApexClass:filedata -
ApexPage:filedata -
CustomField:filedata -
CustomLabel:filedata -
CustomObject:filedata -
Flow:filedata -
NamedCredential:filedata
Referenced By
-
PlatformEventSubscriberConfig:platformEventConsumer. This element belongs to the subscriber configuration, not to the trigger.
Documentation Inconsistencies
- The file-location description names the companion file
TriggerName-meta.xml, while the sample usesMyHelloWorld.trigger-meta.xml. The two descriptions differ on whether the companion filename includes.trigger. - The field definition marks
statusas required, but the sample companion XML omits it. Do not interpret that omission as changing the documented requirement.
Reported Issues
-
An active trigger can still bypass its business logic. A 2019 discussion describes a custom-setting check that returns early from trigger code without changing
statustoInactive. This approach depends on an explicit source-code guard; checking metadata status alone does not reveal it. See the trigger deactivation discussion. - Automated activation and deactivation are still deployment operations. Answers in the same 2019 discussion describe production test coverage and sufficient Metadata API permissions for the calling user as requirements. They do not describe status changes as a way around Apex deployment constraints. See the deployment considerations for status changes.
- An "Invalid type" error can involve deployment-tool configuration. In a 2022 report, a trigger could not resolve its handler class even when the class and trigger were deployed together or the class was deployed first. The author reported success after changing an unspecified deployment-tool setting. This is a tool-specific diagnostic lead, not evidence that a particular Apex visibility change or class-first deployment sequence is generally required. See the handler-class deployment failure.
-
Production trigger removal has been reported through explicit deletion deployments. A 2019 answer describes deploying trigger metadata with
statusset toDeleted. A 2021 answer describes listing trigger names underApexTriggerindestructiveChanges.xml. These are reported removal approaches, not equivalent to settingInactive. See the production deletion discussion.
Common Questions
How do I deactivate an Apex trigger in production?
Set status to Inactive in the companion metadata and deploy the change. The trigger remains present. The activation and deactivation discussion also describes production test coverage and Metadata API permissions as requirements.
Can I retrieve ApexTrigger with readMetadata?
No. ApexTrigger does not support Metadata API CRUD-based calls. Use retrieval for its source and companion metadata, and deployment to apply changes.
Where are Apex trigger files stored?
They are stored in the package's triggers folder. The source uses the .trigger suffix. The official sample names its companion file MyHelloWorld.trigger-meta.xml, although the file-location description uses a different naming pattern.
Why does my trigger report "Invalid type" for a handler class deployed with it?
Check the actual source dependency and deployment scope, but do not assume class-first ordering is the answer. In the 2022 report, both combined and class-first deployments failed; an unspecified deployment-tool setting resolved that case.
Does packageVersions mean the trigger is part of a managed package?
No. packageVersions records installed managed-package versions referenced by the trigger. It describes package dependencies, not the trigger's own package membership.