Apex-dml

提供:Dev Guides
移動先:案内検索

Apex-DML

この章では、Salesforceでさまざまなデータベース変更機能を実行する方法について説明します。 機能を実行できる2つのコメントがあります。

DMLステートメント

DMLは、挿入、更新、削除、アップサート、レコードの復元、レコードのマージ、またはリードの変換操作を実行するために実行されるアクションです。

DMLはApexで最も重要な部分の1つです。ほとんどすべてのビジネスケースがデータベースの変更と修正を伴うためです。

データベースメソッド

DMLステートメントを使用して実行できるすべての操作は、データベースメソッドを使用しても実行できます。 データベースメソッドは、DML操作の実行に使用できるシステムメソッドです。 データベースメソッドは、DMLステートメントに比べて柔軟性が高くなります。

この章では、DMLステートメントを使用した最初のアプローチについて説明します。 後続の章でデータベースメソッドを見ていきます。

DMLステートメント

ここで、化学品の供給会社の例をもう一度考えてみましょう。 請求書レコードには、ステータス、支払額、残額、次回支払日、請求書番号などのフィールドがあります。 今日作成され、ステータスが「保留中」の請求書は、「支払済み」に更新する必要があります。

挿入操作

挿入操作は、データベースに新しいレコードを作成するために使用されます。 Insert DMLステートメントを使用して、標準オブジェクトまたはカスタムオブジェクトのレコードを作成できます。

新しい顧客の注文に対して毎日新しい請求書が生成されるため、APEX_Invoice__cオブジェクトに新しいレコードを作成できます。 最初に顧客レコードを作成してから、その新しい顧客レコードの請求書レコードを作成できます。

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

更新操作

更新操作は、既存のレコードの更新を実行することです。 この例では、既存の請求書レコードのステータスフィールドを「支払い済み」に更新します。

//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]);

アップサート操作

更新挿入操作は、更新操作を実行するために使用され、更新するレコードがデータベースに存在しない場合は、新しいレコードも作成します。

Customerオブジェクトの顧客レコードを更新する必要があるとします。 既存の顧客レコードが既に存在する場合は更新し、そうでない場合は新しいレコードを作成します。 これは、フィールドAPEX_External_Id__cの値に基づきます。 このフィールドは、レコードがすでに存在するかどうかを識別するためのフィールドになります。

-このコードを実行する前に、外部IDフィールドの値を「12341」としてCustomerオブジェクトにレコードを作成し、次に示すコードを実行してください-

//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);
   }
}

削除操作

Delete DMLを使用して削除操作を実行できます。

この場合、テスト目的で作成された請求書、つまり「Test」という名前を含む請求書を削除します。

このスニペットは、クラスを作成せずに開発者コンソールからも実行できます。

//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');

元に戻す操作

削除され、ごみ箱にあるレコードの削除を取り消すことができます。 削除されたレコードが持つすべての関係も復元されます。

前の例で削除されたレコードを復元する必要があるとします。 これは、次の例を使用して実現できます。 前の例のコードは、この例のために変更されています。

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