Apex コードに Salesforce データを使用する
SOQL を使用して Salesforce 組織に対してクエリを実行し、取得したデータをコードで使用します。このステップでは、SOQL を使用して、ボーナス注文品目として追加する商品に関する情報を取得します。

Salesforce からデータを取得するための SOQL を追加する
- クラスの
//TO DO 3.1のコメント (5 行目の近く) の下に次のコードを貼り付けます。// Use SOQL to get the ID of the bonus bouquet and store it in an sObject variable called bonusProduct List<Product2> bonusProductList = [SELECT Id, ProductCode FROM Product2 WHERE ProductCode = 'BOT-BB-12']; Product2 bonusProduct = new Product2(); if(bonusProductList.size() > 0) { bonusProduct = bonusProductList[0]; // Use SOQL to get the price book entry ID associated with the bonusProduct and store it in an sObject variable called entry // Every Product has an assosiated PricebookEntry List<PricebookEntry> entryList = [SELECT Id, Product2Id FROM PricebookEntry WHERE Product2Id = :bonusProduct.Id]; PricebookEntry entry = new PricebookEntry(); if(entryList.size() > 0) { entry = entryList[0]; }
ここでは何を行うのでしょうか?
まず、SOQL を使用して、商品コードが BOT-BB-12 の花束商品の ID と ProductCode を取得し、ID を bonusProductList.というリストに格納します。次に、bonusProductList の項目数を確認します。リストに 1 つ以上の値があれば、新しい Product2 sObject を bonusProduct という名前で定義して、bonusProductList リストにある最初の sObject をその sObject に割り当てます。
次に、別の SOQL クエリを使用して、bonusProduct の PricebookEntry の ID を取得します。PricebookEntry ID は、entryList という名前の PricebookEntry リストに格納します。
最後に、entryList の項目数を確認します。リストに 1 つ以上の値があれば、新しい PricebookEntry sObject を entry という名前で定義して、entryList リストにある最初の sObject をその sObject に割り当てます。
DML を使用して Salesforce にデータを送信する
すでに「システム管理者のためのオブジェクト指向プログラミング」モジュールを修了しているのであれば、DML が Salesforce 組織にデータを送信するために使用するデータ操作言語だということを覚えているでしょう。システム管理者であれば、DML はデータローダーなどのデータ操作ツールで使用する、Insert (レコードの作成)、Update (レコードの編集)、Delete (レコードの削除) ステートメントであると理解しています。
1 つ以上の無料の花束を作成してリストに追加しましたが、まだ Apex コードの状態です。Salesforce 組織には何も保存していません。そこで DML を使用します。

- クラスの
//TO DO 3.2のコメントの下に次のコードを貼り付けます。insert newBouquets;
- クラスの
//TO DO 2.4のコメントの下で、//PricebookEntryId = entry.id,という行からコメント文字 (//) を削除します。 - 最後に、
ifステートメント (ボーナス花束を付与することが決定された場合に実行されるコード) を完成させます。
クラスの//TO DO 3.3のコメントの下に次のコードを貼り付けます。} //end if
- クラスを保存します。
クラスは次のようになります。
//Create the class
public class OrderItemUtility {
//Create the method that will add free bonus bouquet when order is activated
public static void addBonusBouquet(List<Order> ordersFromTrigger) {
//TO DO 3.1: Determine if we have a bonus product and get its ID to add to the order
// Use SOQL to get the ID of the bonus bouquet and store it in an sObject variable called bonusProduct
List<Product2> bonusProductList = [SELECT Id, ProductCode FROM Product2 WHERE ProductCode = 'BOT-BB-12'];
Product2 bonusProduct = new Product2();
if(bonusProductList.size() > 0) {
bonusProduct = bonusProductList[0];
// Use SOQL to get the price book entry ID associated with the bonusProduct and store it in an sObject variable called entry
// Every Product has an assosiated PricebookEntry
List<PricebookEntry> entryList = [SELECT Id, Product2Id FROM PricebookEntry WHERE Product2Id = :bonusProduct.Id];
PricebookEntry entry = new PricebookEntry();
if(entryList.size() > 0) {
entry = entryList[0];
}
//TO DO 2.1: Create a list to store any new bouquets we'll insert later
List<OrderItem> newBouquets = new List<OrderItem>();
//TO DO 2.2: Loop over orders in ordersFromTrigger, for each order (called currentOrder) do something
for(Order currentOrder : ordersFromTrigger) {
//TO DO 2.3: Verify the order status is 'Activated'
if(currentOrder.Status == 'Activated') {
//TO DO 2.4: Create a new bouquet and set values
OrderItem freeBouquet = new OrderItem(
OrderId = currentOrder.id, //this is the order we're linking the bouquet to
PricebookEntryId = entry.id,
numberOfFlowers__c = 3,
description = 'FREE Bouquet',
Quantity = 1,
colorTheme__c = 'Spectacular Sunset',
percentOfOpening__c = 0,
UnitPrice = 0.00
);
//TO DO 2.5: Add the freeBouquet sObject to your list
newBouquets.add(freeBouquet);
//TO DO 2.6: Close the "if" and "for loop" sections
} //end if
} //end for loop
//TO DO 3.2: Use DML to add the new bouquet to the Order
insert newBouquets;
//TO DO 3.3: Close the if section
} //end if
} //end method
} //end class