【Trailhead】Queueable Apex を使用したプロセスの制御

スポンサーリンク
【Trailhead】Queueable Apex を使用した プロセスの制御 Salesforce

こんにちは、にいるです。

今日は、Trailheadのモジュール「Queueable Apex を使用したプロセスの制御」について、解答と解説をまとめたいと思います。

非同期処理について、詳しく説明した記事と合わせてご覧ください。
【Salesforce】非同期処理の特徴と使い方について
【Trailhead】Queueable Apex を使用したプロセスの制御

1.チャレンジ内容

チャレンジ内容です。

1-1.原文

Create an Queueable Apex class that inserts Contacts for Accounts.
Create a Queueable Apex class that inserts the same Contact for each Account for a specific state. Write unit tests that achieve 100% code coverage for the class.

  • Create an Apex class called ‘AddPrimaryContact’ that implements the Queueable interface.
  • Create a constructor for the class that accepts as its first argument a Contact sObject and a second argument as a string for the State abbreviation.
  • The execute method must query for a maximum of 200 Accounts with the BillingState specified by the State abbreviation passed into the constructor and insert the Contact sObject record associated to each Account. Look at the sObject clone() method.
  • Create an Apex test class called ‘AddPrimaryContactTest’.
  • In the test class, insert 50 Account records for BillingState “NY” and 50 Account records for BillingState “CA”. Create an instance of the AddPrimaryContact class, enqueue the job and assert that a Contact record was inserted for each of the 50 Accounts with the BillingState of “CA”.
  • The unit tests must cover all lines of code included in the AddPrimaryContact class, resulting in 100% code coverage.
  • Run your test class at least once (via ‘Run All’ tests the Developer Console) before attempting to verify this challenge.

1-2.和訳

アカウントの連絡先を挿入するQueueableApexクラスを作成します。
特定の状態のアカウントごとに同じ連絡先を挿入するQueueableApexクラスを作成します。クラスのコードカバレッジを100%達成する単体テストを作成します。

  • Queueableインターフェースを実装する「AddPrimaryContact」というApexクラスを作成します。
  • 最初の引数としてContactsObjectを受け入れ、2番目の引数をStateの省略形の文字列として受け入れるクラスのコンストラクターを作成します。
  • executeメソッドは、コンストラクターに渡されたStateの省略形で指定されたBillingStateを使用して最大200のアカウントを照会し、各アカウントに関連付けられたContactsObjectレコードを挿入する必要があります。 sObject clone()メソッドを見てください。
  • ‘AddPrimaryContactTest’という名前のApexテストクラスを作成します。
  • テストクラスで、BillingState “NY”のアカウントレコードを50個とBillingState “CA”のアカウントレコードを50個挿入します。 AddPrimaryContactクラスのインスタンスを作成し、ジョブをキューに入れ、BillingStateが「CA」の50個のアカウントのそれぞれに連絡先レコードが挿入されたことを表明します。
  • 単体テストは、AddPrimaryContactクラスに含まれるすべてのコード行をカバーする必要があり、100%のコードカバレッジが得られます。
  • このチャレンジを検証する前に、テストクラスを少なくとも1回実行します(「すべて実行」を使用して開発者コンソールをテストします)。

2.解答と解説

2-1.Apexクラス

2-2.テストクラス

3.まとめ

いかがでしたでしょうか。

Queueableインターフェースはfutureメソッドの上位互換として使用されます。

他の非同期処理と合わせて体系的に覚えておくのがいいと思います。

皆さんもぜひ色々と触ってみてください。

他にも色々と標準機能やSalesforce機能について紹介していますので、ご覧ください。

ではでは!

Salesforce 機能別 まとめページ

コメント

タイトルとURLをコピーしました