Apex-database-methods
提供:Dev Guides
Apex-データベースメソッド
データベースクラスメソッドは、挿入、更新などのDMLステートメントよりも柔軟なDMLステートメントを操作する別の方法です。
データベースメソッドとDMLステートメントの違い
DML Statements | Database Methods |
---|---|
Partial Update is not allowed. For example, if you have 20 records in list, then either all the records will be updated or none. | Partial update is allowed. You can specify the Parameter in Database method as true or false, true to allow the partial update and false for not allowing the same. |
You cannot get the list of success and failed records. | You can get the list of success and failed records as we have seen in the example. |
Example − insert listName | Example − Database.insert(listName, False), where false indicate that partial update is not allowed. |
挿入操作
データベースメソッドを介して新しいレコードを挿入することも非常に簡単で柔軟です。 DMLステートメントを使用して新しいレコードを挿入した前のシナリオを考えてみましょう。 データベースメソッドを使用して同じものを挿入します。
例
更新操作
データベースメソッドを使用したビジネスケースの例を考えてみましょう。 Invoiceオブジェクトのステータスフィールドを更新する必要があると仮定しますが、同時に、レコードのステータス、失敗したレコードID、成功カウントなどの情報も必要です。 DMLステートメントを使用してこれを行うことはできないため、データベースメソッドを使用して操作のステータスを取得する必要があります。
例
請求書のステータスが「保留中」で、作成日が今日の場合、請求書の「ステータス」フィールドを更新します。
以下に示すコードは、Database.updateメソッドを使用して請求書レコードを更新するのに役立ちます。 また、このコードを実行する前に請求書レコードを作成します。
このチュートリアルでは、挿入と更新の操作のみを見ていきます。 他の操作は、これらの操作と最後の章で行った操作に非常に似ています。