# template generate apex trigger

Generate an Apex trigger.

## Description for template generate apex trigger

Generates the Apex trigger \*.trigger file and associated metadata file. These files must be contained in a parent directory called "triggers" in your package directory. Either run this command from an existing directory of this name, or use the --output-dir flag to generate one or point to an existing one.

If you don't specify the --sobject flag, the .trigger file contains the generic placeholder SOBJECT; replace it with the Salesforce object you want to generate a trigger for. If you don't specify --event, "before insert" is used.

## Aliases for template generate apex trigger

`force:apex:trigger:create`

`apex:generate:trigger`

## Flags

| Flag Name (Long) | Flag Name (Short) | Description                                                                                                                                                                                                                         |
| ---------------- | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--api-version`  | N/A               | **Type:** Value<br><br>Override the api version used for api requests made by this command                                                                                                                                          |
| `--event`        | `-e`              | **Type:** Value<br>**Valid Values:** `before insert`, `before update`, `before delete`, `after insert`, `after update`, `after delete`, `after undelete`<br>**Default value:** `before insert`<br><br>Events that fire the trigger. |
| `--flags-dir`    | N/A               | **Type:** Value<br><br>Import flag values from a directory.                                                                                                                                                                         |
| `--json`         | N/A               | **Type:** Boolean<br><br>Format output as json.                                                                                                                                                                                     |
| `--name`         | `-n`              | **Type:** Value<br>**Required**<br><br>Name of the generated Apex trigger<br><br>The name can be up to 40 characters and must start with a letter.                                                                                  |
| `--output-dir`   | `-d`              | **Type:** Value<br>**Default value:** `.`<br><br>Directory for saving the created files.<br><br>The location can be an absolute path or relative to the current working directory. The default is the current directory.            |
| `--sobject`      | `-s`              | **Type:** Value<br>**Default value:** `SOBJECT`<br><br>Salesforce object to generate a trigger on.                                                                                                                                  |
| `--template`     | `-t`              | **Type:** Value<br>**Valid Values:** `ApexTrigger`<br>**Default value:** `ApexTrigger`<br><br>Template to use for file creation.<br><br>Supplied parameter values or default values are filled into a copy of the template.         |

## Examples for template generate apex trigger

Generate two files associated with the MyTrigger Apex trigger (MyTrigger.trigger and MyTrigger.trigger-meta.xml) in the current directory:

```shell
sf template generate apex trigger --name MyTrigger
```

Similar to the previous example, but generate the files in the "force-app/main/default/triggers" directory:

```shell
sf template generate apex trigger --name MyTrigger --output-dir force-app/main/default/triggers
```

Generate files for a trigger that fires on the Account object before and after an insert:

```shell
sf template generate apex trigger --name MyTrigger --sobject Account --event "before insert,after insert"
```
