0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

シンプルな オブジェクトからレコード検索 サンプル (Salesforce CLI で Anonymous Apex Code 版)

Last updated at Posted at 2021-11-23

ちょっと Salesforce オブジェクトからのレコード検索を Salesforce CLI (sfdx) で動作させてみたい時のサンプルをメモしておきます。
この記事は Salesforce Developer Edition を利用して記載しています。

Annonymous Apex Code の記述

実行したい SOQL を含む Annonymous Apex Code をファイルに記述します。ファイルの拡張子は .apex とします。

myapex.apex

System.debug('シンプルな オブジェクト検索: Begin.');
List<Account> result = [
    SELECT Id, Name
    FROM Account
    WHERE IsDeleted = false
    ORDER BY Name LIMIT 5
];
for (Account look : result) {
    System.debug('Account: ' + look.Name);
}
System.debug('シンプルな オブジェクト検索: End.');

簡易な解説

  • SOQL を実行して Account オブジェクトを検索
  • 検索結果を繰り返し Debug 出力

Annonymous Apex Code の実行

Salesforce CLI (sfdx) ログイン

Salesforce CLI (sfdx) でログインします。

sfdx auth:web:login -a myorg1

Salesforce CLI (sfdx) で Annonymous Apex Code の実行

先ほど作成した Annonymous Apex Code ファイルを指定して実行します。

sfdx force:apex:execute -u myorg1 -f myapex.apex

実行結果

実行結果の例を以下に示します。

Compiled successfully.
Executed successfully.

53.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO
Execute Anonymous: System.debug('シンプルな オブジェクト検索: Begin.');
Execute Anonymous: List<Account> result = [
Execute Anonymous:     SELECT Id, Name
Execute Anonymous:     FROM Account
Execute Anonymous:     WHERE IsDeleted = false
Execute Anonymous:     ORDER BY Name LIMIT 5
Execute Anonymous: ];
Execute Anonymous: for (Account look : result) {
Execute Anonymous:     System.debug('Account: ' + look.Name);
Execute Anonymous: }
Execute Anonymous: System.debug('シンプルな オブジェクト検索: End.');
19:59:00.118 (118862407)|USER_INFO|[EXTERNAL]|XXX|XXX|(GMT+09:00) Japan Standard Time (Asia/Tokyo)|GMT+09:00
19:59:00.118 (118887772)|EXECUTION_STARTED
19:59:00.118 (118892038)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
19:59:00.118 (119539571)|USER_DEBUG|[1]|DEBUG|シンプルな オブジェクト検索: Begin.
19:59:00.118 (126194215)|USER_DEBUG|[9]|DEBUG|Account: ABC Genius Tech Consulting
19:59:00.118 (126250048)|USER_DEBUG|[9]|DEBUG|Account: Acme Corporation
19:59:00.118 (126269647)|USER_DEBUG|[9]|DEBUG|Account: Apples & Oranges
19:59:00.118 (126286499)|USER_DEBUG|[9]|DEBUG|Account: Barbary Coast Wireless
19:59:00.118 (126301386)|USER_DEBUG|[9]|DEBUG|Account: Blackbeards Grog Emporium
19:59:00.118 (126320806)|USER_DEBUG|[11]|DEBUG|シンプルな オブジェクト検索: End.
19:59:00.126 (126421098)|CUMULATIVE_LIMIT_USAGE
19:59:00.126 (126421098)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 5 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of Publish Immediate DML: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

19:59:00.126 (126421098)|CUMULATIVE_LIMIT_USAGE_END

19:59:00.118 (126466029)|CODE_UNIT_FINISHED|execute_anonymous_apex
19:59:00.118 (126475652)|EXECUTION_FINISHED

文書情報

  • 初出: 2021-11-23

関連情報

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?