匿名 Apex の実行
apex run Salesforce CLI コマンドを使用すると、組織内で Apex コードの匿名ブロックを実行できます。
- ターミナル (macOS および Linux) またはコマンドプロンプト (Windows) を開き、Salesforce DX プロジェクトディレクトリに移動します。
-
apex run コマンドをフラグなしで実行して、対話型シェルを開きます。プロンプトが表示されたら、Apex コードをすべて入力します。終了したら CTRL+D キーを押します。コードが、指定した組織やデフォルトの組織 (組織を指定しない場合) で、1 回の匿名実行要求で実行されます。
1sf apex run --target-org myscratch次の出力は、Apex コード system.debug ('Hello world!'); の実行例を示しています。1Start typing Apex code. Press the Enter key after each line, then press CTRL+D when finished. 2system.debug ('Hello world!'); 3Compiled successfully. 4Executed successfully. 5 658.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO 7Execute Anonymous: system.debug ('Hello world!'); 814:23:06.174 (174742273)|USER_INFO|[EXTERNAL]|0058H000005QWcE|test-ux9lpg9jyyqt@example.com|(GMT-07:00) Pacific Daylight Time (America/Los_Angeles)|GMT-07:00 914:23:06.174 (174785450)|EXECUTION_STARTED 1014:23:06.174 (174792639)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex 1114:23:06.174 (175417814)|USER_DEBUG|[1]|DEBUG|Hello world! 1214:23:06.175 (175529797)|CUMULATIVE_LIMIT_USAGE 1314:23:06.175 (175529797)|LIMIT_USAGE_FOR_NS|(default)| 14 Number of SOQL queries: 0 out of 100 15 Number of query rows: 0 out of 50000 16 Number of SOSL queries: 0 out of 20 17 Number of DML statements: 0 out of 150 18 Number of Publish Immediate DML: 0 out of 150 19 Number of DML rows: 0 out of 10000 20 Maximum CPU time: 0 out of 10000 21 Maximum heap size: 0 out of 6000000 22 Number of callouts: 0 out of 100 23 Number of Email Invocations: 0 out of 10 24 Number of future calls: 0 out of 50 25 Number of queueable jobs added to the queue: 0 out of 50 26 Number of Mobile Apex push calls: 0 out of 10 27 2814:23:06.175 (175529797)|CUMULATIVE_LIMIT_USAGE_END 29 3014:23:06.174 (175598235)|CODE_UNIT_FINISHED|execute_anonymous_apex 3114:23:06.174 (175617689)|EXECUTION_FINISHEDApex コードを対話型で実行するのではなく、ファイル内のコードを実行するには、-file フラグを使用します。
1sf apex run --file ~/test.apex