無料の花束プロモーションを実行するオートメーションを作成する
このステップでは、すべての注文の状況を確認するための新しいクラスと新しいメソッドを作成します。注文が有効化されると、注文にボーナス花束を自動的に追加します。
コードを見るときは、// で始まるテキストはコードのコメントであることを忘れないでください。コードでは、コメントでコードブロック (1 行以上のコード) の目的を説明します。コメントは、記述すべきコード、既存のコードの処理、失敗したコードが実行するはずだった処理を開発者が理解するのに役立ちます。
クラスとメソッドを作成する
-
をクリックして、[Setup (設定)] を選択します。
- [Quick Find (クイック検索)] ボックスで [Web Console (Web コンソール)] を検索して選択します。
- [Enable Developer Console (開発者コンソールを有効化)] で、[Active (有効)] に設定します。
- ページを更新します。
-
をクリックして、[Developer Console (開発者コンソール)] を選択します。
-
[File (ファイル)] | [New (新規)] | [Apex Class (Apex クラス)] をクリックします。
- このクラスに
OrderItemUtilityという名前を付けて、[OK] をクリックします。
- 既存のコードを次のコードに置き換えます。
//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 //TO DO 2.1: Create a list to store any new bouquets we'll insert later //TO DO 2.2: Loop over orders in ordersFromTrigger, for each order (called currentOrder) do something //TO DO 2.3: Verify the order status is 'Activated' //TO DO 2.4: Create a new bouquet and set values //TO DO 2.5: Add the freeBouquet sObject to your list //TO DO 2.6: Close the "if" and "for loop" sections //TO DO 3.2: Use DML to add the new list of newBouquets //TO DO 3.3: Close the if section } //end method } //end class -
[File (ファイル)] | [Save (保存)] をクリックして、クラスを保存します。
新しい花束を作成するためのオートメーションを追加する
次に、オートメーションをクラスに追加します。コードに追加の行を貼り付けると、コードコメント (//) が空白行に追加されます。コードを貼り付けるときには、余分なコメントを忘れずに削除してください。
- 新しいボーナス花束を格納するためのリストを作成します。
クラス内のコメント//TO DO 2.1の下に、次のコードを貼り付けます。List<OrderItem> newBouquets = new List<OrderItem>();
-
forループを使用して、トリガーから送られる注文を反復処理します。(トリガーは後で作成します。)
クラス内のコメント//TO DO 2.2の下に、次のコードを貼り付けます。for(Order currentOrder : ordersFromTrigger) {
This is the first part of the for loop. It takes all the orders from the trigger and adds them to theordersFromTriggerlist. The for loop evaluates each order and names the ordercurrentOrder.
- 次に各注文を (作成した
currentOrder変数を使用して) 調べ、その注文が有効化されているかどうかを確認します。有効化されていれば、ほかの処理を行います。有効化されていなければ、次の注文で同じ確認を行います。
クラス内のコメント//TO DO 2.3の下に、次のコードを貼り付けます。if(currentOrder.Status == 'Activated') {forループのコードと同じように、開き中括弧しかありません。有効化されている各注文の処理が完了した後で、閉じ中括弧を追加します。
有効化されている注文を見つけたら、新しいOrderItem(注文品目) の sObject を作成します。
クラス内のコメント//TO DO 2.4の下に、次のコードを貼り付けます。
-
ここでは
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 );freeBouquetという OrderItem (注文品目) sObject を作成し、その項目に値を割り当てます。//TO DO 2.1で作成したリストに sObject を追加します。
クラス内のコメント//TO DO 2.5の下に、次のコードを貼り付けます。
-
newBouquets.add(freeBouquet);
We're using theaddmethod that's built into Apex, making it easy to add an item to a list.
In this statement,newBouquetsis the name of the list, add is the method, andfreeBouquet(in parentheses) is the sObject that is added to thenewBouquetslist.
- Finally, we need to end the
ifstatement (the code that runs only for activated orders) and theforloop (the code that runs for each order as the loop cycles through all activated orders).
In your class, under the comment//TO DO 2.6, paste this code:クラスファイルは次のようになります。} //end if } //end for loop
//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 //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 //TO DO 3.3: Close the if section } //end method } //end class -
[File (ファイル)] | [Save (保存)] をクリックして、
OrderItemUtilityクラスを保存します。
次の単元では、Salesforce からデータを取得したり Salesforce にデータを送信したりする方法を学習し、トリガーを作成します。そしていよいよコードをテストします。
