📅  最后修改于: 2020-11-05 03:15:06             🧑  作者: Mango
数据库类方法是使用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语句插入了新记录。我们将使用数据库方法插入相同的内容。
// 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 InvoiceListToInsert = new List();
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语句来实现,因此我们必须使用Database方法获得我们的运营状态。
如果发票的状态为“待处理”且创建日期为今天,我们将更新发票的“状态”字段。
下面给出的代码将有助于使用Database.update方法更新发票记录。另外,在执行此代码之前,请创建一个发票记录。
// Code to update the records using the Database methods
List invoiceList = [SELECT id, Name, APEX_Status__c,
createdDate FROM APEX_Invoice__c WHERE createdDate = today];
// fetch the invoice created today
List updatedInvoiceList = new List();
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());
}
}
}
在本教程中,我们将仅关注插入和更新操作。其他操作与这些操作以及我们在上一章所做的操作非常相似。