數(shù)據(jù)庫類方法是使用DML語句的另一種方法,它比DML語句更靈活,如插入,更新等。
DML語句 | 數(shù)據(jù)庫方法 |
---|---|
不允許部分更新。 例如,如果列表中有20條記錄,則所有記錄都將更新或不更新。 | 允許部分更新。 您可以在Database方法中指定參數(shù)為true或false,true表示允許部分更新,false表示不允許相同。 |
您無法獲取成功和失敗記錄的列表。 | 您可以獲取成功和失敗記錄的列表,如我們在示例中看到的。 |
示例:insert listName; | 示例:Database.insert(listName,F(xiàn)alse),其中false表示不允許部分更新。 |
通過數(shù)據(jù)庫方法插入新記錄也非常簡單和靈活。 讓我們采用前面的場景,其中我們使用DML語句插入了新的記錄。 我們將插入相同的使用數(shù)據(jù)庫方法。
例如:
//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()); } } }
讓我們來看看使用數(shù)據(jù)庫方法的業(yè)務案例示例。 假設我們想更新Invoice對象的狀態(tài)字段,但是同時,我們希望有諸如記錄狀態(tài),失敗的記錄ID,成功計數(shù)等信息。這是不可能通過使用DML語句完成的,因此我們必須使用數(shù)據(jù)庫方法來獲取我們操作的狀態(tài)。
例如:
如果發(fā)票的狀態(tài)為“待處理”,且創(chuàng)建日期為今天,我們將更新發(fā)票的“狀態(tài)”字段。
//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()); } } }我們將只看本教程中的插入和更新操作.。 其他操作與這些操作非常相似,我們在上一章中做過了。
更多建議: