Skip to main content

Apex コードに Salesforce データを使用する

メモ

メモ

日本語で受講されている方へ
このバッジの Trailhead ハンズオン Challenge は英語で受講していただく必要があります。英語の意味についてはかっこ内の翻訳をご参照ください。必ず英語の値をコピーして貼り付けてから、Trailhead Playground の言語を [English] に、ロケールを [United States] に切り替えてください。こちらの指示に従ってください。

翻訳版の Trailhead を活用する方法については、自分の言語の Trailhead バッジをご覧ください。

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

Salesforce の注文オブジェクトを表すアイコンから「SOQL」と書かれた矢印が出ていて、クエリと一致するデータのテーブルを指している。

Salesforce からデータを取得するための SOQL を追加する

  1. クラスの //TO DO 3.1 のヘッダー (7 行目の近く) の下に次のコードを貼り付けます。
    // 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 に割り当てます。

メモ

リストの項目番号は常に 0 から開始されるため、bonusProductList[0] はリストの最初の項目を指します。

次に、別の 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 を使用します。

「OrderItem sObject」というラベルが付いた花束から「DML」と書かれた矢印が出ていて、「Record saved in Salesforce OrderItem object (Salesforce OrderItem (注文品目) オブジェクトに保存されているレコード)」というラベルが付いたクラウド内の OrderItem (注文品目) アイコンを指している。

  1. クラスの //TO DO 3.2 のヘッダー (50 行目の近く) の下に次のコードを貼り付けます。
    insert newBouquets;
  2. クラスの //TO DO 2.4 のヘッダー (34 行目の近く) の下で、//PricebookEntryId = entry.id, という行からコメント文字 ( // ) を削除します。
  3. 最後に、if ステートメント (ボーナス花束を付与することが決定された場合に実行されるコード) を完成させます。

    クラスの //TO DO 3.3 のヘッダー (53 行目の近く) の下に次のコードを貼り付けます。

    } //end if
  4. クラスを保存します。

クラスは次のようになります。

//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
無料で学習を続けましょう!
続けるにはアカウントにサインアップしてください。
サインアップすると次のような機能が利用できるようになります。
  • 各自のキャリア目標に合わせてパーソナライズされたおすすめが表示される
  • ハンズオン Challenge やテストでスキルを練習できる
  • 進捗状況を追跡して上司と共有できる
  • メンターやキャリアチャンスと繋がることができる