Skip to main content
The Trailblazer Community will undergo maintenance on Saturday, November 15, 2025 and Sunday, November 16, 2025. Please plan your activities accordingly.

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

メモ

メモ

日本語で受講されている方へ
Challenge は日本語の Trailhead Playground で開始し、かっこ内の翻訳を参照しながら進めていってください。Challenge での評価は英語データを対象に行われるため、英語の値のみをコピーして貼り付けるようにしてください。日本語の組織で Challenge が不合格だった場合は、(1) この手順に従って [Locale (地域)] を [United States (米国)] に切り替え、(2) [Language (言語)] を [English (英語)] に切り替えてから、(3) [Check Challenge (Challenge を確認)] ボタンをクリックしてみることをお勧めします。

翻訳版 Trailhead を活用する方法の詳細は、自分の言語の Trailhead バッジを参照してください。

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

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

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

  1. クラスの //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 に割り当てます。

メモ

リストの項目番号は常に 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 のコメントの下に次のコードを貼り付けます。
    insert newBouquets;
  2. クラスの //TO DO 2.4 のコメントの下で、//PricebookEntryId = entry.id, という行からコメント文字 ( // ) を削除します。
  3. 最後に、if ステートメント (ボーナス花束を付与することが決定された場合に実行されるコード) を完成させます。
    クラスの //TO DO 3.3 のコメントの下に次のコードを貼り付けます。
    } //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
Salesforce ヘルプで Trailhead のフィードバックを共有してください。

Trailhead についての感想をお聞かせください。[Salesforce ヘルプ] サイトから新しいフィードバックフォームにいつでもアクセスできるようになりました。

詳細はこちら フィードバックの共有に進む