
- Apex程式設計教程
- Apex - 首頁
- Apex - 概述
- Apex - 環境
- Apex - 示例
- Apex - 資料型別
- Apex - 變數
- Apex - 字串
- Apex - 陣列
- Apex - 常量
- Apex - 決策制定
- Apex - 迴圈
- Apex - 集合
- Apex - 類
- Apex - 方法
- Apex - 物件
- Apex - 介面
- Apex - DML
- Apex - 資料庫方法
- Apex - SOSL
- Apex - SOQL
- Apex - 安全性
- Apex - 呼叫
- Apex - 觸發器
- Apex - 觸發器設計模式
- Apex - 限制
- Apex - 批處理
- Apex - 除錯
- Apex - 測試
- Apex - 部署
- Apex有用資源
- Apex - 快速指南
- Apex - 資源
- Apex - 討論
Apex - 資料庫方法
資料庫類方法是處理DML語句的另一種方式,它比insert、update等DML語句更靈活。
資料庫方法和DML語句的區別
DML語句 | 資料庫方法 |
---|---|
不允許部分更新。例如,如果列表中有20條記錄,則要麼所有記錄都更新,要麼都不更新。 | 允許部分更新。您可以在資料庫方法中將引數指定為true或false,true表示允許部分更新,false表示不允許。 |
您無法獲取成功和失敗記錄的列表。 | 您可以獲取成功和失敗記錄的列表,正如我們在示例中看到的。 |
示例 - insert listName | 示例 - Database.insert(listName, False),其中false表示不允許部分更新。 |
插入操作
透過資料庫方法插入新記錄也很簡單且靈活。讓我們考慮之前的場景,我們使用DML語句插入了新記錄。我們將使用資料庫方法插入相同的記錄。
示例
// Insert Operation Using Database methods // Insert Customer Records First using simple DML Statement. This Customer Record will be // used when we will create Invoice Records APEX_Customer__c objCust = new APEX_Customer__C(); objCust.Name = 'Test'; insert objCust; // Inserting the Customer Records // Insert Operation Using Database methods APEX_Invoice__c objNewInvoice = new APEX_Invoice__c(); List<apex_invoice__c> InvoiceListToInsert = new List<apex_invoice__c>(); objNewInvoice.APEX_Status__c = 'Pending'; objNewInvoice.APEX_Customer__c = objCust.id; objNewInvoice.APEX_Amount_Paid__c = 1000; InvoiceListToInsert.add(objNewInvoice); Database.SaveResult[] srList = Database.insert(InvoiceListToInsert, false); // Database method to insert the records in List // Iterate through each returned result by the method for (Database.SaveResult sr : srList) { if (sr.isSuccess()) { // This condition will be executed for successful records and will fetch the ids // of successful records System.debug('Successfully inserted Invoice. Invoice ID: ' + sr.getId()); // Get the invoice id of inserted Account } else { // This condition will be executed for failed records for(Database.Error objErr : sr.getErrors()) { System.debug('The following error has occurred.'); // Printing error message in Debug log System.debug(objErr.getStatusCode() + ': ' + objErr.getMessage()); System.debug('Invoice oject field which are affected by the error:' + objErr.getFields()); } } }
更新操作
現在讓我們使用資料庫方法考慮我們的業務案例示例。假設我們需要更新Invoice物件的status欄位,但同時我們還需要諸如記錄狀態、失敗記錄ID、成功計數等資訊。這無法透過使用DML語句實現,因此我們必須使用資料庫方法來獲取操作的狀態。
示例
如果Invoice的“狀態”為“待處理”且建立日期為今天,我們將更新其“狀態”欄位。
以下程式碼將幫助使用Database.update方法更新Invoice記錄。此外,在執行此程式碼之前,請建立Invoice記錄。
// Code to update the records using the Database methods List<apex_invoice__c> invoiceList = [SELECT id, Name, APEX_Status__c, createdDate FROM APEX_Invoice__c WHERE createdDate = today]; // fetch the invoice created today List<apex_invoice__c> updatedInvoiceList = new List<apex_invoice__c>(); for (APEX_Invoice__c objInvoice: invoiceList) { if (objInvoice.APEX_Status__c == 'Pending') { objInvoice.APEX_Status__c = 'Paid'; updatedInvoiceList.add(objInvoice); //Adding records to the list } } Database.SaveResult[] srList = Database.update(updatedInvoiceList, false); // Database method to update the records in List // Iterate through each returned result by the method for (Database.SaveResult sr : srList) { if (sr.isSuccess()) { // This condition will be executed for successful records and will fetch // the ids of successful records System.debug('Successfully updated Invoice. Invoice ID is : ' + sr.getId()); } else { // This condition will be executed for failed records for(Database.Error objErr : sr.getErrors()) { System.debug('The following error has occurred.'); // Printing error message in Debug log System.debug(objErr.getStatusCode() + ': ' + objErr.getMessage()); System.debug('Invoice oject field which are affected by the error:' + objErr.getFields()); } } }
在本教程中,我們將僅關注插入和更新操作。其他操作與此類似,與上一章中我們所做的操作類似。
廣告