课程表

Apex课程

工具箱
速查手册

Apex - 数据库方法

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

数据库类方法是使用DML语句的另一种方法,它比DML语句更灵活,如插入,更新等。

数据库方法和DML语句之间的差异

DML语句数据库方法
不允许部分更新。 例如,如果列表中有20条记录,则所有记录都将更新或不更新。允许部分更新。 您可以在Database方法中指定参数为true或false,true表示允许部分更新,false表示不允许相同。
您无法获取成功和失败记录的列表。您可以获取成功和失败记录的列表,如我们在示例中看到的。
示例:insert listName;示例:Database.insert(listName,False),其中false表示不允许部分更新。


INSERT插入操作

通过数据库方法插入新记录也非常简单和灵活。 让我们采用前面的场景,其中我们使用DML语句插入了新的记录。 我们将插入相同的使用数据库方法。


例如:

  1. //Insert Operation Using Database methods
  2. //Insert Customer Records First using simple DML Statement. This Customer Record will be used when we will create Invoice Records
  3. APEX_Customer__c objCust = new APEX_Customer__C();
  4. objCust.Name = 'Test';
  5. insert objCust;//Inserting the Customer Records
  6. //Insert Operation Using Database methods
  7. APEX_Invoice__c objNewInvoice = new APEX_Invoice__c();
  8. List<apex_invoice__c> InvoiceListToInsert = new List<apex_invoice__c>();
  9. objNewInvoice.APEX_Status__c = 'Pending';
  10. objNewInvoice.APEX_Customer__c = objCust.id;
  11. objNewInvoice.APEX_Amount_Paid__c = 1000;
  12. InvoiceListToInsert.add(objNewInvoice);
  13. Database.SaveResult[] srList = Database.insert(InvoiceListToInsert, false);//Database method to insert the records in List
  14. // Iterate through each returned result by the method
  15. for (Database.SaveResult sr : srList) {
  16. if (sr.isSuccess()) {
  17.     // This condition will be executed for successful records and will fetch the ids of successful records
  18. System.debug('Successfully inserted Invoice. Invoice ID: ' + sr.getId());//Get the invoice id of inserted Account
  19. }
  20. else {
  21.     // This condition will be executed for failed records
  22. for(Database.Error objErr : sr.getErrors()) {
  23. System.debug('The following error has occurred.');  //Printing error message in Debug log
  24. System.debug(objErr.getStatusCode() + ': ' + objErr.getMessage());
  25. System.debug('Invoice oject field which are affected by the error: ' + objErr.getFields());
  26. }
  27. }
  28. }


UPDATE更新操作

让我们来看看使用数据库方法的业务案例示例。 假设我们想更新Invoice对象的状态字段,但是同时,我们希望有诸如记录状态,失败的记录ID,成功计数等信息。这是不可能通过使用DML语句完成的,因此我们必须使用数据库方法来获取我们操作的状态。


例如:

如果发票的状态为“待处理”,且创建日期为今天,我们将更新发票的“状态”字段。

下面的代码是使用Database.update方法更新发票记录。 此外,在执行此代码之前创建发票记录。

  1. //Code to update the records using the Database methods
  2. List<apex_invoice__c> invoiceList = [SELECT id, Name, APEX_Status__c, createdDate FROM APEX_Invoice__c WHERE createdDate = today];//fetch the invoice created today
  3. List<apex_invoice__c> updatedInvoiceList = new List<apex_invoice__c>();
  4. for (APEX_Invoice__c objInvoice: invoiceList) {
  5. if (objInvoice.APEX_Status__c == 'Pending') {
  6. objInvoice.APEX_Status__c = 'Paid';
  7. updatedInvoiceList.add(objInvoice);//Adding records to the list
  8. }
  9. }
  10. Database.SaveResult[] srList = Database.update(updatedInvoiceList, false);//Database method to update the records in List
  11.  
  12.  
  13. // Iterate through each returned result by the method
  14. for (Database.SaveResult sr : srList) {
  15. if (sr.isSuccess()) {
  16.     // This condition will be executed for successful records and will fetch the ids of successful records
  17.     System.debug('Successfully updated Invoice. Invoice ID is : ' + sr.getId());
  18. }
  19. else {
  20.     // This condition will be executed for failed records
  21. for(Database.Error objErr : sr.getErrors()) {
  22. System.debug('The following error has occurred.');//Printing error message in Debug log
  23. System.debug(objErr.getStatusCode() + ': ' + objErr.getMessage());
  24. System.debug('Invoice oject field which are affected by the error: ' + objErr.getFields());
  25. }
  26. }
  27. }

我们将只看本教程中的插入和更新操作.。 其他操作与这些操作非常相似,我们在上一章中做过了。
转载本站内容时,请务必注明来自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号