【Trailhead】Apex スケジューラを使用したジョブのスケジュール

スポンサーリンク
【Trailhead】 Apex スケジューラを使用した ジョブのスケジュール Salesforce

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

今日は、Trailheadのモジュール「Apex スケジューラを使用したジョブのスケジュール」について、解答と解説をまとめたいと思います。

非同期処理について、詳しく説明した記事と合わせてご覧ください。
【Salesforce】非同期処理の特徴と使い方について
【Trailhead】Apex スケジューラを使用したジョブのスケジュール

1.チャレンジ内容

チャレンジ内容です。

1-1.原文

Create an Apex class that uses Scheduled Apex to update Lead records.
Create an Apex class that implements the Schedulable interface to update Lead records with a specific LeadSource. Write unit tests that achieve 100% code coverage for the class. This is very similar to what you did for Batch Apex.

  • Create an Apex class called ‘DailyLeadProcessor’ that uses the Schedulable interface.
  • The execute method must find the first 200 Leads with a blank LeadSource field and update them with the LeadSource value of ‘Dreamforce’.
  • Create an Apex test class called ‘DailyLeadProcessorTest’.
  • In the test class, insert 200 Lead records, schedule the DailyLeadProcessor class to run and test that all Lead records were updated correctly.
  • The unit tests must cover all lines of code included in the DailyLeadProcessor 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.和訳

スケジュールされたApexを使用してリードレコードを更新するApexクラスを作成します。
特定のLeadSourceでLeadレコードを更新するSchedulableインターフェースを実装するApexクラスを作成します。 クラスのコードカバレッジを100%達成する単体テストを作成します。 これは、BatchApexで行ったことと非常によく似ています。

  • Schedulableインターフェースを使用する「DailyLeadProcessor」というApexクラスを作成します。
  • executeメソッドは、LeadSourceフィールドが空白の最初の200件のリードを検索し、それらを「Dreamforce」のLeadSource値で更新する必要があります。
  • ‘DailyLeadProcessorTest’というApexテストクラスを作成します。
  • テストクラスで、200のリードレコードを挿入し、DailyLeadProcessorクラスを実行するようにスケジュールして、すべてのリードレコードが正しく更新されたことをテストします。
  • 単体テストは、DailyLeadProcessorクラスに含まれるコードのすべての行をカバーする必要があり、100%のコードカバレッジが得られます。
  • このチャレンジを検証する前に、テストクラスを少なくとも1回実行します(「すべて実行」を使用して開発者コンソールをテストします)。

2.解答と解説

2-1.Apexクラス

2-2.テストクラス

3.まとめ

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

Apexスケジューラは非同期処理で最もよく使うインターフェースです。

一括処理と併用すれば大量のレコードを定期実行で処理できるので是非とも覚えておきたい構文です。

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

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

ではでは!

Salesforce 機能別 まとめページ

コメント

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