正解:D
* Database.insert(records, false):
* The Database.insert() method with the allOrNone parameter set to false allows for partial success.
* If some records in the list fail due to validation rules, triggers, or other errors, the method will still attempt to insert the remaining valid records.
* The false parameter ensures that records that fail will not roll back the transaction for the others.
* Why not the other options?
* A. Database.insert(records, true):
* The true parameter makes the operation transactional (all or none). If any record fails, all records will roll back.
* B. insert records:
* The insert DML statement behaves like Database.insert(records, true) by default and rolls back all records if any error occurs.
* C. insert(records, false):
* This syntax is invalid in Apex.
References:
* Apex DML Operations Documentation
* Database Methods
最新のコメント (最新のコメントはトップにあります。)
A. Database.insert(records, true)
B. insert records
C. insert (records, false)
D. Database.insert(records, false) ★
正解は **D** です。
(※選択肢の日本語訳が機械翻訳風になっていますが、**`Database.insert(records, false);`** を指しています。)
[cite\_start]提供されたドキュメントの「部分コミット」のセクション [cite: 902-904] に基づき解説します。
### 正解の解説
**D. データベース。レコードを挿入します (false) → `Database.insert(list, false);`**
* **部分コミット (Partial Commit):** リスト内のレコードを保存する際、エラーが発生したレコードだけを失敗させ、**問題のないレコードは正常に保存**させたい場合に使用します。
* **構文:** `Database` クラスのメソッドを使用し、第2引数(`allOrNone` パラメータ)に **`false`** を指定します。
```apex
// 部分的な成功を許可する
Database.SaveResult[] results = Database.insert(recordList, false);
```
* [cite\_start]この場合、例外(Exception)はスローされず、戻り値の `SaveResult` を確認して成功・失敗を判定します [cite: 905-906]。
-----
### 不正解の解説
**B. レコードを挿入する → `insert recordList;`**
* これは標準の DML ステートメントです。
* [cite\_start]デフォルトで **"All or Nothing"(全か無か)** の挙動となります。リスト内の1件でもエラーがあれば、**すべての処理がロールバック(失敗)** され、例外 (`DmlException`) が発生します [cite: 896, 899-900]。
**A. データベース。レコードを挿入(true) → `Database.insert(list, true);`**
* [cite\_start]第2引数を `true` にすると、標準の `insert` ステートメント(B)と同じく **"All or Nothing"** になります。エラーがあれば全件失敗します [cite: 898-900]。
**C. レコードを挿入 (false)**
* これは Apex の構文として存在しません(DMLステートメント `insert` は引数を取りません)。
### 💡 学習のポイント
**「一部失敗しても、残りは保存したい」** と言われたら:
👉 **`Database.insert(list, false)`** (または `Database.update`, `Database.upsert` 等の `false` 指定)
これ一択です。...