Apex - DML
在本章中,我们将讨论如何在 Salesforce 中执行不同的数据库修改功能。 我们可以通过两个命令来执行这些功能。
DML 语句
DML 是为了执行插入、更新、删除、更新插入、恢复记录、合并记录或转换潜在客户操作而执行的操作。
DML是Apex中最重要的部分之一,因为几乎每个业务案例都涉及对数据库的更改和修改。
数据库方法
可以使用 DML 语句执行的所有操作也可以使用数据库方法执行。 数据库方法是可用于执行 DML 操作的系统方法。 与 DML 语句相比,数据库方法提供了更大的灵活性。
在本章中,我们将研究使用 DML 语句的第一种方法。 我们将在后续章节中讨论数据库方法。
DML 语句
现在让我们再次考虑化学品供应商公司的实例。 我们的账单记录包含状态、已付金额、剩余金额、下一个付款日期和账单编号等字段。 今天创建且状态为"待处理"的账单应更新为"已付款"。
插入操作
插入操作用于在数据库中创建新记录。 您可以使用 Insert DML 语句创建任何标准或自定义对象的记录。
示例
我们可以在 APEX_Invoice__c 对象中创建新记录,因为每天都会为新客户订单生成新账单。 我们将首先创建一个客户记录,然后我们可以为该新客户记录创建一个账单记录。
// 获取今天创建的账单,注意,您必须至少有一张今天创建的账单
List<apex_invoice__c> invoiceList = [SELECT id, Name, APEX_Status__c,
createdDate FROM APEX_Invoice__c WHERE createdDate = today];
// 创建List来保存更新的账单记录
List<apex_invoice__c> updatedInvoiceList = new List<apex_invoice__c>();
APEX_Customer__c objCust = new APEX_Customer__C();
objCust.Name = 'Test ABC';
//用于插入新客户记录的DML
insert objCust;
for (APEX_Invoice__c objInvoice: invoiceList) {
if (objInvoice.APEX_Status__c == 'Pending') {
objInvoice.APEX_Status__c = 'Paid';
updatedInvoiceList.add(objInvoice);
}
}
// 更新账单状态的 DML 语句
update updatedInvoiceList;
// 打印更新后的账单的值
System.debug('List has been updated and updated values are' + updatedInvoiceList);
// 使用insert DML语句插入新记录
APEX_Invoice__c objNewInvoice = new APEX_Invoice__c();
objNewInvoice.APEX_Status__c = 'Pending';
objNewInvoice.APEX_Amount_Paid__c = 1000;
objNewInvoice.APEX_Customer__c = objCust.id;
// DML 正在创建新的账单记录,该记录将与新链接
// 创建客户记录
insert objNewInvoice;
System.debug('New Invoice Id is '+objNewInvoice.id+' and the Invoice Number is'
+ objNewInvoice.Name);
更新操作
更新操作是对现有记录进行更新。 在此示例中,我们将现有账单记录的状态字段更新为"已付款"。
示例
// 更新账单状态的更新语句示例。 在执行此代码之前,您必须创建账单记录。 该程序正在更新位于列表索引 0 位置的记录。
// 首先,获取今天创建的账单
List<apex_invoice__c> invoiceList = [SELECT id, Name, APEX_Status__c,
createdDate FROM APEX_Invoice__c];
List<apex_invoice__c> updatedInvoiceList = new List<apex_invoice__c>();
//更新List中的第一条记录
invoiceList[0].APEX_Status__c = 'Pending';
updatedInvoiceList.add(invoiceList[0]);
// 更新账单状态的 DML 语句
update updatedInvoiceList;
// 打印更新后的账单的值
System.debug('List has been updated and updated values of records are'
+ updatedInvoiceList[0]);
更新插入操作
Upsert操作用于执行更新操作,如果要更新的记录不存在于数据库中,则也创建新记录。
示例
假设,Customer对象中的客户记录需要更新。 如果现有客户记录已存在,我们将更新该记录,否则创建一条新记录。 这将基于字段 APEX_External_Id__c 的值。 该字段将是我们用来识别记录是否已存在的字段。
注意 − 在执行此代码之前,请在 Customer 对象中创建一条记录,外部 Id 字段值为"12341",然后执行下面给出的代码 −
// 更新插入客户记录的示例
List<apex_customer__c> CustomerList = new List<apex_customer__c>();
for (Integer i = 0; i < 10; i++) {
apex_customer__c objcust=new apex_customer__c(name = 'Test' +i,
apex_external_id__c='1234' +i);
customerlist.add(objcust);
} //更新客户记录
upsert CustomerList;
System.debug('Code iterated for 10 times and created 9 records as one record with
External Id 12341 is already present');
for (APEX_Customer_c objCustomer: CustomerList) {
if (objCustomer.APEX_External_Id_c == '12341') {
system.debug('The Record which is already present is '+objCustomer);
}
}
删除操作
您可以使用删除 DML 执行删除操作。
示例
在这种情况下,我们将删除为测试目的而创建的账单,即包含名称为"Test"的账单。
您也可以从开发者控制台执行此代码段,而无需创建类。
// 获取今天创建的账单
List<apex_invoice__c> invoiceList = [SELECT id, Name, APEX_Status__c,
createdDate FROM APEX_Invoice__c WHERE createdDate = today];
List<apex_invoice__c> updatedInvoiceList = new List<apex_invoice__c>();
APEX_Customer__c objCust = new APEX_Customer__C();
objCust.Name = 'Test';
// 插入客户记录
insert objCust;
for (APEX_Invoice__c objInvoice: invoiceList) {
if (objInvoice.APEX_Status__c == 'Pending') {
objInvoice.APEX_Status__c = 'Paid';
updatedInvoiceList.add(objInvoice);
}
}
// 更新账单状态的 DML 语句
update updatedInvoiceList;
// 打印更新后的账单的值
System.debug('List has been updated and updated values are' + updatedInvoiceList);
// 使用insert DML语句插入新记录
APEX_Invoice__c objNewInvoice = new APEX_Invoice__c();
objNewInvoice.APEX_Status__c = 'Pending';
objNewInvoice.APEX_Amount_Paid__c = 1000;
objNewInvoice.APEX_Customer__c = objCust.id;
// 正在创建新记录的 DML
insert objNewInvoice;
System.debug('New Invoice Id is' + objNewInvoice.id);
// 从数据库中删除测试账单获取为测试创建的账单,选择客户名称为测试的名称。
List<apex_invoice__c> invoiceListToDelete = [SELECT id FROM APEX_Invoice__c
WHERE APEX_Customer__r.Name = 'Test'];
// 删除账单的 DML 语句
delete invoiceListToDelete;
System.debug('Success, '+invoiceListToDelete.size()+' Records has been deleted');
取消删除操作
您可以取消删除已删除且存在于回收站中的记录。 被删除的记录所具有的所有关系也将被恢复。
示例
假设需要恢复上例中删除的记录。 这可以使用以下示例来实现。 上一个示例中的代码已针对本示例进行了修改。
// 获取今天创建的账单
List<apex_invoice__c> invoiceList = [SELECT id, Name, APEX_Status__c,
createdDate FROM APEX_Invoice__c WHERE createdDate = today];
List<apex_invoice__c> updatedInvoiceList = new List<apex_invoice__c>();
APEX_Customer__c objCust = new APEX_Customer__C();
objCust.Name = 'Test';
// 插入客户记录
insert objCust;
for (APEX_Invoice__c objInvoice: invoiceList) {
if (objInvoice.APEX_Status__c == 'Pending') {
objInvoice.APEX_Status__c = 'Paid';
updatedInvoiceList.add(objInvoice);
}
}
// 更新账单状态的 DML 语句
update updatedInvoiceList;
// 打印更新后的账单的值
System.debug('List has been updated and updated values are' + updatedInvoiceList);
// Inserting the New Records using insert DML statement
APEX_Invoice__c objNewInvoice = new APEX_Invoice__c();
objNewInvoice.APEX_Status__c = 'Pending';
objNewInvoice.APEX_Amount_Paid__c = 1000;
objNewInvoice.APEX_Customer__c = objCust.id;
// 正在创建新记录的 DML
insert objNewInvoice;
System.debug('New Invoice Id is '+objNewInvoice.id);
// 从数据库中删除测试账单获取为测试创建的账单,选择客户名称为测试的名称。
List<apex_invoice__c> invoiceListToDelete = [SELECT id FROM APEX_Invoice__c
WHERE APEX_Customer__r.Name = 'Test'];
// 删除账单的 DML 语句
delete invoiceListToDelete;
system.debug('Deleted Record Count is ' + invoiceListToDelete.size());
System.debug('Success, '+invoiceListToDelete.size() + 'Records has been deleted');
// 使用undelete语句恢复删除的记录
undelete invoiceListToDelete;
System.debug('Undeleted Record count is '+invoiceListToDelete.size()+'. This should
be same as Deleted Record count');

