この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

Trigger クラス

トリガの種類、トリガの操作対象となる sObject レコードのリストなど、トリガのランタイムコンテキスト情報にアクセスするには、Trigger クラスを使用します。

名前空間

System

トリガコンテキスト変数

Trigger クラスは次のコンテキスト変数を提供します。
変数 使用方法
isExecuting Apex コードの現在のコンテキストが Visualforce ページ、Web サービス、または executeanonymous() API コールではなく、トリガである場合、true を返します。
isInsert 挿入操作により、Salesforce ユーザインターフェース、Apex、または API からこのトリガが実行された場合に、true を返します。
isUpdate 更新操作により、Salesforce ユーザインターフェース、Apex、または API からこのトリガが実行された場合に、true を返します。
isDelete 削除操作により、Salesforce ユーザインターフェース、Apex、または API からこのトリガが実行された場合に、true を返します。
isBefore レコードが保存される前にこのトリガが実行された場合に、true を返します。
isAfter すべてのレコードが保存された後にこのトリガが実行された場合に、true を返します。
isUndelete レコードがごみ箱から復元された後 (Salesforce ユーザインターフェース、Apex、または API からの復元操作の後) にこのトリガが実行された場合に、true を返します。
new 新しいバージョンの sObject レコードのリストを返します。

この sObject リストは insert トリガと update トリガでのみ使用でき、レコードは before トリガでのみ更新できます。

newMap 新しいバージョンの sObject レコードへの ID の対応付けです。

この対応付けは before update トリガ、after insert トリガ、after update トリガでのみ使用できます。

old 古いバージョンの sObject レコードのリストを返します。

この sObject リストは update トリガと delete トリガでのみ使用できます。

oldMap 古いバージョンの sObject レコードへの ID の対応付けです。

この対応付けは update トリガと delete トリガでのみ使用できます。

size 古いバージョンと新しいバージョンの両方を含む、トリガ呼び出しのレコードの合計数。

トリガを実行するレコードに無効な項目値がある場合 (たとえば、0 で割る数式など)、値は newnewMapold、および oldMap のトリガコンテキスト変数で null に設定されます。

メモ

たとえば、この単純なトリガの場合、Trigger.new は sObject のリストで、for ループで繰り返し実行でき、SOQL クエリの IN 句でバインド変数として使用できます。

1Trigger simpleTrigger on Account (after insert) {
2    for (Account a : Trigger.new) {
3        // Iterate over each sObject
4    }
5
6    // This single query finds every contact that is associated with any of the
7    // triggering accounts. Note that although Trigger.new is a collection of  
8    // records, when used as a bind variable in a SOQL query, Apex automatically
9    // transforms the list of records into a list of corresponding Ids.
10    Contact[] cons = [SELECT LastName FROM Contact
11                      WHERE AccountId IN :Trigger.new];
12}

このトリガでは、Trigger.isBeforeTrigger.isDelete のような Boolean コンテキスト変数を使用して、特定のトリガ条件でのみ実行するコードを定義します。

1trigger myAccountTrigger on Account(before delete, before insert, before update, 
2                                    after delete, after insert, after update) {
3if (Trigger.isBefore) {
4    if (Trigger.isDelete) {
5
6        // In a before delete trigger, the trigger accesses the records that will be
7        // deleted with the Trigger.old list.
8        for (Account a : Trigger.old) {
9            if (a.name != 'okToDelete') {
10                a.addError('You can\'t delete this record!');
11            } 
12        }
13    } else {
14
15    // In before insert or before update triggers, the trigger accesses the new records
16    // with the Trigger.new list.
17        for (Account a : Trigger.new) {
18            if (a.name == 'bad') {
19                a.name.addError('Bad name');
20            }
21    }
22    if (Trigger.isInsert) {
23        for (Account a : Trigger.new) {
24            System.assertEquals('xxx', a.accountNumber); 
25            System.assertEquals('industry', a.industry); 
26            System.assertEquals(100, a.numberofemployees);
27            System.assertEquals(100.0, a.annualrevenue);
28            a.accountNumber = 'yyy';
29        }
30
31// If the trigger is not a before trigger, it must be an after trigger.
32} else {
33    if (Trigger.isInsert) {
34        List<Contact> contacts = new List<Contact>();
35        for (Account a : Trigger.new) {        
36            if(a.Name == 'makeContact') {
37                contacts.add(new Contact (LastName = a.Name,
38                                          AccountId = a.Id));
39            }
40        } 
41      insert contacts;
42    }
43  }
44}}}