- Apex 编程教程
- 顶点 - 主页
- Apex - 概述
- 顶点 - 环境
- 顶点 - 示例
- Apex - 数据类型
- Apex - 变量
- Apex - 弦乐
- Apex - 数组
- Apex - 常量
- Apex - 决策
- 顶点 - 循环
- Apex - 集合
- Apex - 课程
- Apex - 方法
- Apex - 对象
- Apex - 接口
- Apex-DML
- Apex - 数据库方法
- 顶点 - SOSL
- 顶点-SOQL
- 顶点 - 安全
- Apex - 调用
- Apex - 触发器
- Apex - 触发器设计模式
- Apex - 调节器限制
- Apex - 批处理
- Apex - 调试
- Apex - 测试
- Apex - 部署
- Apex 有用资源
- Apex - 快速指南
- Apex - 资源
- Apex - 讨论
Apex - 数据库方法
数据库类方法是使用 DML 语句的另一种方式,它比插入、更新等 DML 语句更灵活。
数据库方法和DML语句的区别
| DML 语句 | 数据库方法 |
|---|---|
| 不允许部分更新。例如,如果列表中有 20 条记录,则要么所有记录都将被更新,要么不更新。 | 允许部分更新。您可以将 Database 方法中的参数指定为 true 或 false,true 表示允许部分更新, false 表示不允许部分更新。 |
| 您无法获取成功和失败记录的列表。 | 您可以获得成功和失败记录的列表,正如我们在示例中看到的那样。 |
| 示例- 插入列表名称 | 示例- 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 对象的状态字段,但同时我们还需要记录状态、失败记录 ID、成功计数等信息。这是使用 DML 语句无法实现的,因此我们必须使用数据库方法获取我们的运营状态。
例子
如果发票的状态为“待处理”且创建日期为今天,我们将更新发票的“状态”字段。
下面给出的代码将有助于使用 Database.update 方法更新发票记录。另外,在执行此代码之前创建一个发票记录。
// 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());
}
}
}
在本教程中我们将只讨论插入和更新操作。其他操作与这些操作和我们在上一章中所做的非常相似。