Apex - DML

2019-10-26 16:26 更新

在Salesforce中,我們可以通過兩種方式執(zhí)行所有數(shù)據(jù)庫修改功能:


DML語句

DML是為了執(zhí)行插入,更新,刪除,上升,恢復(fù)記錄,合并記錄或轉(zhuǎn)換引線操作而執(zhí)行的動作。

DML是Apex中最重要的部分之一,因?yàn)閹缀趺總€業(yè)務(wù)案例都涉及對數(shù)據(jù)庫的更改和修改。


數(shù)據(jù)庫方法

您可以使用DML語句執(zhí)行的所有操作也可以使用數(shù)據(jù)庫方法執(zhí)行。數(shù)據(jù)庫方法是可以用于執(zhí)行DML操作的系統(tǒng)方法。與DML語句相比,數(shù)據(jù)庫方法提供了更多的靈活性。

在本章中,我們將討論使用DML語句的第一種方法。我們將在下一章討論數(shù)據(jù)庫方法。


DML語句

讓我們再次舉一個化學(xué)品供應(yīng)商公司的例子。我們的發(fā)票記錄具有狀態(tài),已支付金額,剩余金額,下一個工資日期和發(fā)票編號。今天創(chuàng)建且狀態(tài)為“待處理”的發(fā)票應(yīng)更新為“已付款”。


INSERT操作

插入操作用于在數(shù)據(jù)庫中創(chuàng)建新記錄。您可以使用插入DML語句創(chuàng)建任何標(biāo)準(zhǔn)或自定義對象的記錄。


例如:

我們想在APEX_Invoice__c對象中創(chuàng)建一些新記錄,因?yàn)槊刻鞛樾驴蛻粲唵紊尚掳l(fā)票。我們將首先創(chuàng)建一個客戶記錄,然后我們可以為該新客戶記錄創(chuàng)建一個發(fā)票記錄。

//fetch the invoices created today, Note, you must have at least one invoice created today
List<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 records
List<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 Records
insert 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 status
update updatedInvoiceList;

//Prints the value of updated invoices
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 which is creating the new Invoice record which will be linked with newly created Customer record
insert objNewInvoice;
System.debug('New Invoice Id is '+objNewInvoice.id+' and the Invoice Number is '+objNewInvoice.Name);


更新操作

更新操作現(xiàn)有記錄進(jìn)行更新示例,我們更新現(xiàn)有發(fā)票記錄收費(fèi)狀態(tài)字段

例如:

//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 today
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>();

//Update the first record in the List
invoiceList[0].APEX_Status__c = 'Pending';
updatedInvoiceList.add(invoiceList[0]);

//DML Statement to update the invoice status
update updatedInvoiceList;

//Prints the value of updated invoices
System.debug('List has been updated and updated values of records are'+updatedInvoiceList[0]);


Upsert操作

Upsert操作用于執(zhí)行更新操作,并且如果要更新的記錄不存在于數(shù)據(jù)庫中,則也會創(chuàng)建新記錄。


例如:

假設(shè),我們想更新Customer對象中的客戶記錄。 但是,如果現(xiàn)有的客戶記錄已經(jīng)存在,我們想更新它,則要創(chuàng)建一個新的客戶記錄。 這將基于字段APEX_External_Id__c的值。 此字段將是我們的字段,用于標(biāo)識記錄是否已存在。


注:在執(zhí)行此代碼之前,請?jiān)谕獠縄d字段值為“12341”的Customer對象中創(chuàng)建一條記錄,然后執(zhí)行以下代碼:

//Example for upserting the Customer records
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);
        } //Upserting the Customer Records

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執(zhí)行刪除操作。


例如:

在這種情況下,我們想刪除為測試目的創(chuàng)建的發(fā)票,即包含名稱為“Test”的發(fā)票。

您可以從開發(fā)人員控制臺執(zhí)行此代碼段,而不創(chuàng)建類。

//fetch the invoice created today
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';
//Inserting the Customer Records
insert 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 status
update updatedInvoiceList;

//Prints the value of updated invoices
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 which is creating the new record
insert objNewInvoice;
System.debug('New Invoice Id is '+objNewInvoice.id);

//Deleting the Test invoices from Database
//fetch the invoices which are created for Testing, Select name which Customer Name is Test.
List<apex_invoice__c> invoiceListToDelete = [SELECT id FROM APEX_Invoice__c WHERE APEX_Customer__r.Name = 'Test'];

//DML Statement to delete the Invoices
delete invoiceListToDelete;
System.debug('Success, '+invoiceListToDelete.size()+' Records has been deleted');


取消刪除操作

您可以取消刪除已刪除并存在于回收站中的記錄。 刪除的記錄具有的所有關(guān)系也將被恢復(fù)。


例如:

假設(shè),您要恢復(fù)上一個示例中刪除的記錄。 這可以使用以下示例來實(shí)現(xiàn)。 我們修改了前面的例子,并在這里添加了一些額外的代碼。

//fetch the invoice created today
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';

//Inserting the Customer Records
insert 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 status
update updatedInvoiceList;

//Prints the value of updated invoices
System.debug('List has been updated and updated values are'+updatedInvoiceList);
	
//Inserting the New Records using insert DML statemnt
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 which is creating the new record
insert objNewInvoice;
System.debug('New Invoice Id is '+objNewInvoice.id);

//Deleting the Test invoices from Database
//fetch the invoices which are created for Testing, Select name which Customer Name is Test.
List<apex_invoice__c> invoiceListToDelete = [SELECT id FROM APEX_Invoice__c WHERE APEX_Customer__r.Name = 'Test'];

//DML Statement to delete the Invoices
delete invoiceListToDelete;
system.debug('Deleted Record Count is '+invoiceListToDelete.size());
System.debug('Success, '+invoiceListToDelete.size()+'Records has been deleted');	

//Restore the deleted records using undelete statement
undelete invoiceListToDelete;
System.debug('Undeleted Record count is '+invoiceListToDelete.size()+'. This should be same as Deleted Record count');

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號