当前位置:K88软件开发文章中心编程语言非主流编程语言Apex → 文章内容

Apex - DML

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 16:30:51

由 kaikai0220 创建,youj 最后一次修改 2016-12-12 在Salesforce中,我们可以通过两种方式执行所有数据库修改功能: DML语句 DML是为了执行插入,更新,删除,上升,恢复记录,合并记录或转换引线操作而执行的动作。DML是Apex中最重要的部分之一,因为几乎每个业务案例都涉及对数据库的更改和修改。 数据库方法您可以使用DML语句执行的所有操作也可以使用数据库方法执行。数据库方法是可以用于执行DML操作的系统方法。与DML语句相比,数据库方法提供了更多的灵活性。在本章中,我们将讨论使用DML语句的第一种方法。我们将在下一章讨论数据库方法。 DML语句让我们再次举一个化学品供应商公司的例子。我们的发票记录具有状态,已支付金额,剩余金额,下一个工资日期和发票编号。今天创建且状态为“待处理”的发票应更新为“已付款”。 INSERT操作插入操作用于在数据库中创建新记录。您可以使用插入DML语句创建任何标准或自定义对象的记录。 例如: 我们想在APEX_Invoice__c对象中创建一些新记录,因为每天为新客户订单生成新发票。我们将首先创建一个客户记录,然后我们可以为该新客户记录创建一个发票记录。 //fetch the invoices created today, Note, you must have at least one invoice created todayList<apex_invoice__c> invoiceList = [SELECT id, Name, APEX_Status__c, createdDate FROM APEX_Invoice__c WHERE createdDate = today];//create List to hold the updated invoice recordsList<apex_invoice__c> updatedInvoiceList = new List<apex_invoice__c>();APEX_Customer__c objCust = new APEX_Customer__C();objCust.Name = 'Test ABC';//DML for Inserting the new Customer Recordsinsert objCust;for (APEX_Invoice__c objInvoice: invoiceList) {if (objInvoice.APEX_Status__c == 'Pending') {objInvoice.APEX_Status__c = 'Paid';updatedInvoiceList.add(objInvoice);}}//DML Statement to update the invoice statusupdate updatedInvoiceList;//Prints the value of updated invoicesSystem.debug('List has been updated and updated values are'+updatedInvoiceList);//Inserting the New Records using insert DML statementAPEX_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 which is creating the new Invoice record which will be linked with newly created Customer recordinsert objNewInvoice;System.debug('New Invoice Id is '+objNewInvoice.id+' and the Invoice Number is '+objNewInvoice.Name); 更新操作更新操作是对现有记录进行更新。在此示例中,我们将更新现有发票记录到收费的状态字段。 例如: //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.//First, fetch the invoice created todayList<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>();//Update the first record in the ListinvoiceList[0].APEX_Status__c = 'Pending';updatedInvoiceList.add(invoiceList[0]);//DML Statement to update the invoice statusupdate updatedInvoiceList;//Prints the value of updated invoicesSystem.debug('List has been updated and updated values of records are'+updatedInvoiceList[0]);Upsert操作Upsert操作用于执行更新操作,并且如果要更新的记录不存在于数据库中,则也会创建新记录。 例如: 假设,我们想更新Customer对象中的客户记录。 但是,如果现有的客户记录已经存在,我们想更新它,则要创建一个新的客户记录。 这将基于字段APEX_External_Id__c的值。 此字段将是我们的字段,用于标识记录是否已存在。 注:在执行此代码之前,请在外部Id字段值为“12341”的Customer对象中创建一条记录,然后执行以下代码: //Example for upserting the Customer recordsList<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);        } //Upserting the Customer Recordsupsert 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”的发票。您可以从开发人员控制台执行此代码段,而不创建类。 //fetch the invoice created todayList<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';//Inserting the Customer Recordsinsert objCust;for (APEX_Invoice__c objInvoice: invoiceList) {    if (objInvoice.APEX_Status__c == 'Pending') {    objInvoice.APEX_Status__c = 'Paid';    updatedInvoiceList.add(objInvoice);    }}//DML Statement to update the invoice statusupdate updatedInvoiceList;//Prints the value of updated invoicesSystem.debug('List has been updated and updated values are'+updatedInvoiceList);//Inserting the New Records using insert DML statementAPEX_Invoice__

[1] [2]  下一页


Apex - DML