【Trailhead】Apex 一括処理の使用

スポンサーリンク
【Trailhead】Apex 一括処理の使用 Salesforce

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

今日は、Trailheadのモジュール「Apex 一括処理の使用」について、解答と解説をまとめたいと思います。

非同期処理について、詳しく説明した記事と合わせてご覧ください。
【Salesforce】非同期処理の特徴と使い方について
【Trailhead】Apex 一括処理の使用

1.チャレンジ内容

チャレンジ内容です。

1-1.原文

Create an Apex class that uses Batch Apex to update Lead records.
Create an Apex class that implements the Database.Batchable interface to update all Lead records in the org with a specific LeadSource. Write unit tests that achieve 100% code coverage for the class.

  • Create an Apex class called ‘LeadProcessor’ that uses the Database.Batchable interface.
  • Use a QueryLocator in the start method to collect all Lead records in the org.
  • The execute method must update all Lead records in the org with the LeadSource value of ‘Dreamforce’.
  • Create an Apex test class called ‘LeadProcessorTest’.
  • In the test class, insert 200 Lead records, execute the ‘LeadProcessor’ Batch class and test that all Lead records were updated correctly.
  • The unit tests must cover all lines of code included in the LeadProcessor 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.和訳

BatchApexを使用してリードレコードを更新するApexクラスを作成します。
Database.Batchableインターフェイスを実装するApexクラスを作成して、組織内のすべてのLeadレコードを特定のLeadSourceで更新します。 クラスのコードカバレッジを100%達成する単体テストを作成します。

  • Database.Batchableインターフェイスを使用する「LeadProcessor」というApexクラスを作成します。
  • startメソッドでQueryLocatorを使用して、組織内のすべてのリードレコードを収集します。
  • executeメソッドは、組織内のすべてのLeadレコードを「Dreamforce」のLeadSource値で更新する必要があります。
  • ‘LeadProcessorTest’というApexテストクラスを作成します。
  • テストクラスで、200個のLeadレコードを挿入し、「LeadProcessor」バッチクラスを実行して、すべてのLeadレコードが正しく更新されたことをテストします。
  • 単体テストは、LeadProcessorクラスに含まれるコードのすべての行をカバーする必要があり、100%のコードカバレッジが得られます。
  • このチャレンジを検証する前に、テストクラスを少なくとも1回実行します(「すべて実行」を使用して開発者コンソールをテストします)。

2.解答と解説

2-1.Apexクラス

これは見た通り、startメソッドでデータを取得して、executeで実装したい処理を記述してるだけですね。

finishは何も書いていないですが、メール通知やexecuteの更新数を渡して通知に使うことができます。
statefulを使用すれば各トランザクション間で値保持もできます。

2-2.テストクラス

3.まとめ

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

バッチ処理はApexの非同期処理で最も使用する頻度が高い構文だと思います。

一度、書いてしまえばなかなか書くことはないと思いますが、構文だけでもしっかりと覚えておきましょう。

皆さんもぜひ色々と試してみてください。

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

ではでは!

Salesforce 機能別 まとめページ

コメント

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