课程表

Apex课程

工具箱
速查手册

Apex - DML

当前位置:免费教程 » 程序设计 » Apex

在Salesforce中,我们可以通过两种方式执行所有数据库修改功能:


DML语句

DML是为了执行插入,更新,删除,上升,恢复记录,合并记录或转换引线操作而执行的动作。

DML是Apex中最重要的部分之一,因为几乎每个业务案例都涉及对数据库的更改和修改。


数据库方法

您可以使用DML语句执行的所有操作也可以使用数据库方法执行。数据库方法是可以用于执行DML操作的系统方法。与DML语句相比,数据库方法提供了更多的灵活性。

在本章中,我们将讨论使用DML语句的第一种方法。我们将在下一章讨论数据库方法。


DML语句

让我们再次举一个化学品供应商公司的例子。我们的发票记录具有状态,已支付金额,剩余金额,下一个工资日期和发票编号。今天创建且状态为“待处理”的发票应更新为“已付款”。


INSERT操作

插入操作用于在数据库中创建新记录。您可以使用插入DML语句创建任何标准或自定义对象的记录。


例如:

我们想在APEX_Invoice__c对象中创建一些新记录,因为每天为新客户订单生成新发票。我们将首先创建一个客户记录,然后我们可以为该新客户记录创建一个发票记录。

  1. //fetch the invoices created today, Note, you must have at least one invoice created today
  2. List<apex_invoice__c> invoiceList = [SELECT id, Name, APEX_Status__c, createdDate FROM APEX_Invoice__c WHERE createdDate = today];
  3.  
  4. //create List to hold the updated invoice records
  5. List<apex_invoice__c> updatedInvoiceList = new List<apex_invoice__c>();
  6. APEX_Customer__c objCust = new APEX_Customer__C();
  7. objCust.Name = 'Test ABC';
  8.  
  9. //DML for Inserting the new Customer Records
  10. insert objCust;
  11. for (APEX_Invoice__c objInvoice: invoiceList) {
  12. if (objInvoice.APEX_Status__c == 'Pending') {
  13. objInvoice.APEX_Status__c = 'Paid';
  14. updatedInvoiceList.add(objInvoice);
  15. }
  16. }
  17.  
  18. //DML Statement to update the invoice status
  19. update updatedInvoiceList;
  20.  
  21. //Prints the value of updated invoices
  22. System.debug('List has been updated and updated values are'+updatedInvoiceList);
  23. //Inserting the New Records using insert DML statement
  24. APEX_Invoice__c objNewInvoice = new APEX_Invoice__c();
  25. objNewInvoice.APEX_Status__c = 'Pending';
  26. objNewInvoice.APEX_Amount_Paid__c = 1000;
  27. objNewInvoice.APEX_Customer__c = objCust.id;
  28.  
  29. //DML which is creating the new Invoice record which will be linked with newly created Customer record
  30. insert objNewInvoice;
  31. System.debug('New Invoice Id is '+objNewInvoice.id+' and the Invoice Number is '+objNewInvoice.Name);


更新操作

更新操作现有记录进行更新示例我们更新现有发票记录收费状态字段

例如:

  1. //Update Statement Example for updating the invoice status. You have to create and Invoice records before executing this code. This program is updating the record which is at index 0th position of the List.
  2. //First, fetch the invoice created today
  3. List<apex_invoice__c> invoiceList = [SELECT id, Name, APEX_Status__c, createdDate FROM APEX_Invoice__c];
  4. List<apex_invoice__c> updatedInvoiceList = new List<apex_invoice__c>();
  5.  
  6. //Update the first record in the List
  7. invoiceList[0].APEX_Status__c = 'Pending';
  8. updatedInvoiceList.add(invoiceList[0]);
  9.  
  10. //DML Statement to update the invoice status
  11. update updatedInvoiceList;
  12.  
  13. //Prints the value of updated invoices
  14. System.debug('List has been updated and updated values of records are'+updatedInvoiceList[0]);


Upsert操作

Upsert操作用于执行更新操作,并且如果要更新的记录不存在于数据库中,则也会创建新记录。


例如:

假设,我们想更新Customer对象中的客户记录。 但是,如果现有的客户记录已经存在,我们想更新它,则要创建一个新的客户记录。 这将基于字段APEX_External_Id__c的值。 此字段将是我们的字段,用于标识记录是否已存在。


注:在执行此代码之前,请在外部Id字段值为“12341”的Customer对象中创建一条记录,然后执行以下代码:

  1. //Example for upserting the Customer records
  2. List<apex_customer__c> CustomerList = new List<apex_customer__c>();
  3. for (Integer i=0; i< 10; i++) {
  4.         apex_customer__c objcust=new apex_customer__c(name='Test' +i, apex_external_id__c='1234' +i);
  5.         customerlist.add(objcust);
  6.         } //Upserting the Customer Records
  7.  
  8. upsert CustomerList;
  9.  
  10. System.debug('Code iterated for 10 times and created 9 records as one record with External Id 12341 is already present');
  11.  
  12. for (APEX_Customer__c objCustomer: CustomerList) {
  13. if (objCustomer.APEX_External_Id__c == '12341') {
  14. system.debug('The Record which is already present is '+objCustomer);
  15. }
  16. }


删除操作

你可以使用删除DML执行删除操作。


例如:

在这种情况下,我们想删除为测试目的创建的发票,即包含名称为“Test”的发票。

您可以从开发人员控制台执行此代码段,而不创建类。

  1. //fetch the invoice created today
  2. List<apex_invoice__c> invoiceList = [SELECT id, Name, APEX_Status__c, createdDate FROM APEX_Invoice__c WHERE createdDate = today];
  3. List<apex_invoice__c> updatedInvoiceList = new List<apex_invoice__c>();
  4. APEX_Customer__c objCust = new APEX_Customer__C();
  5. objCust.Name = 'Test';
  6. //Inserting the Customer Records
  7. insert objCust;
  8. for (APEX_Invoice__c objInvoice: invoiceList) {
  9.     if (objInvoice.APEX_Status__c == 'Pending') {
  10.    objInvoice.APEX_Status__c = 'Paid';
  11.    updatedInvoiceList.add(objInvoice);
  12.     }
  13. }
  14.  
  15. //DML Statement to update the invoice status
  16. update updatedInvoiceList;
  17.  
  18. //Prints the value of updated invoices
  19. System.debug('List has been updated and updated values are'+updatedInvoiceList);
  20. //Inserting the New Records using insert DML statement
  21. APEX_Invoice__c objNewInvoice = new APEX_Invoice__c();
  22. objNewInvoice.APEX_Status__c = 'Pending';
  23. objNewInvoice.APEX_Amount_Paid__c = 1000;
  24. objNewInvoice.APEX_Customer__c = objCust.id;
  25.  
  26. //DML which is creating the new record
  27. insert objNewInvoice;
  28. System.debug('New Invoice Id is '+objNewInvoice.id);
  29.  
  30. //Deleting the Test invoices from Database
  31. //fetch the invoices which are created for Testing, Select name which Customer Name is Test.
  32. List<apex_invoice__c> invoiceListToDelete = [SELECT id FROM APEX_Invoice__c WHERE APEX_Customer__r.Name = 'Test'];
  33.  
  34. //DML Statement to delete the Invoices
  35. delete invoiceListToDelete;
  36. System.debug('Success, '+invoiceListToDelete.size()+' Records has been deleted');


取消删除操作

您可以取消删除已删除并存在于回收站中的记录。 删除的记录具有的所有关系也将被恢复。


例如:

假设,您要恢复上一个示例中删除的记录。 这可以使用以下示例来实现。 我们修改了前面的例子,并在这里添加了一些额外的代码。

  1. //fetch the invoice created today
  2. List<apex_invoice__c> invoiceList = [SELECT id, Name, APEX_Status__c, createdDate FROM APEX_Invoice__c WHERE createdDate = today];
  3. List<apex_invoice__c> updatedInvoiceList = new List<apex_invoice__c>();
  4. APEX_Customer__c objCust = new APEX_Customer__C();
  5. objCust.Name = 'Test';
  6.  
  7. //Inserting the Customer Records
  8. insert objCust;
  9. for (APEX_Invoice__c objInvoice: invoiceList) {
  10. if (objInvoice.APEX_Status__c == 'Pending') {
  11. objInvoice.APEX_Status__c = 'Paid';
  12. updatedInvoiceList.add(objInvoice);
  13. }
  14. }
  15.  
  16. //DML Statement to update the invoice status
  17. update updatedInvoiceList;
  18.  
  19. //Prints the value of updated invoices
  20. System.debug('List has been updated and updated values are'+updatedInvoiceList);
  21. //Inserting the New Records using insert DML statemnt
  22. APEX_Invoice__c objNewInvoice = new APEX_Invoice__c();
  23. objNewInvoice.APEX_Status__c = 'Pending';
  24. objNewInvoice.APEX_Amount_Paid__c = 1000;
  25. objNewInvoice.APEX_Customer__c = objCust.id;
  26.  
  27. //DML which is creating the new record
  28. insert objNewInvoice;
  29. System.debug('New Invoice Id is '+objNewInvoice.id);
  30.  
  31. //Deleting the Test invoices from Database
  32. //fetch the invoices which are created for Testing, Select name which Customer Name is Test.
  33. List<apex_invoice__c> invoiceListToDelete = [SELECT id FROM APEX_Invoice__c WHERE APEX_Customer__r.Name = 'Test'];
  34.  
  35. //DML Statement to delete the Invoices
  36. delete invoiceListToDelete;
  37. system.debug('Deleted Record Count is '+invoiceListToDelete.size());
  38. System.debug('Success, '+invoiceListToDelete.size()+'Records has been deleted');
  39.  
  40. //Restore the deleted records using undelete statement
  41. undelete invoiceListToDelete;
  42. System.debug('Undeleted Record count is '+invoiceListToDelete.size()+'. This should be same as Deleted Record count');
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号