Newer Version Available
トリガコンテキスト変数
すべてのトリガは、開発者がランタイムコンテキストにアクセスできるようにする暗黙的な変数を定義します。これらの変数は、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 | 古いバージョンと新しいバージョンの両方を含む、トリガ呼び出しのレコードの合計数。 |
たとえば、この単純なトリガの場合、Trigger.new は sObject のリストで、for ループで繰り返し実行でき、SOQL クエリの IN 句でバインド変数として使用できます。
1swfobject.registerObject("clippy.trigger_variables_ex", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17Trigger simpleTrigger on Account (after insert) {
18 for (Account a : Trigger.new) {
19 // Iterate over each sObject
20 }
21
22 // This single query finds every contact that is associated with any of the
23 // triggering accounts. Note that although Trigger.new is a collection of
24 // records, when used as a bind variable in a SOQL query, Apex automatically
25 // transforms the list of records into a list of corresponding Ids.
26 Contact[] cons = [SELECT LastName FROM Contact
27 WHERE AccountId IN :Trigger.new];
28}このトリガでは、Trigger.isBefore や Trigger.isDelete のような Boolean コンテキスト変数を使用して、特定のトリガ条件でのみ実行するコードを定義します。
1swfobject.registerObject("clippy.trigger_variables_ex2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17trigger myAccountTrigger on Account(before delete, before insert, before update,
18 after delete, after insert, after update) {
19if (Trigger.isBefore) {
20 if (Trigger.isDelete) {
21
22 // In a before delete trigger, the trigger accesses the records that will be
23 // deleted with the Trigger.old list.
24 for (Account a : Trigger.old) {
25 if (a.name != 'okToDelete') {
26 a.addError('You can\'t delete this record!');
27 }
28 }
29 } else {
30
31 // In before insert or before update triggers, the trigger accesses the new records
32 // with the Trigger.new list.
33 for (Account a : Trigger.new) {
34 if (a.name == 'bad') {
35 a.name.addError('Bad name');
36 }
37 }
38 if (Trigger.isInsert) {
39 for (Account a : Trigger.new) {
40 System.assertEquals('xxx', a.accountNumber);
41 System.assertEquals('industry', a.industry);
42 System.assertEquals(100, a.numberofemployees);
43 System.assertEquals(100.0, a.annualrevenue);
44 a.accountNumber = 'yyy';
45 }
46
47// If the trigger is not a before trigger, it must be an after trigger.
48} else {
49 if (Trigger.isInsert) {
50 List<Contact> contacts = new List<Contact>();
51 for (Account a : Trigger.new) {
52 if(a.Name == 'makeContact') {
53 contacts.add(new Contact (LastName = a.Name,
54 AccountId = a.Id));
55 }
56 }
57 insert contacts;
58 }
59 }
60}}}
61