Module: SfCli::Sf::Apex::Run

Included in:
Core
Defined in:
lib/sf_cli/sf/apex/run.rb

Defined Under Namespace

Classes: ApexResult

Instance Method Summary collapse

Instance Method Details

#run(target_org: nil, file: nil) ⇒ Object

run apex code and returns its result

target_org — an alias of paticular org, or username can be used

file — (1) path to a local file that contains Apex code. (2) StringIO object

Examples

Execute apex code in a file

result = sf.apex.run file: "path/to/apex"
result.success # true if the execution succeeds
result.logs    # execution log

StringIO is usable instead of file path

require 'stringio'

pseudo_file = StringIO.new <<~EOS
  Account acc = [SELECT Id, Name FROM Account Limit 1];
  System.debug(acc.Name);
EOS

sf.apex.run target_org: :dev, file: pseudo_file

Interactive Mode

If you don’t specify file argument, it starts interactive mode that may be helpful in IRB environment.

irb(main:) > sf.apex.run target_org: :dev

Account acc = [SELECT Id, Name FROM Account LIMIT 1];
System.debug(acc.Name);
<press Ctrl-D>

=>
#<SfCli::Sf::Apex::Run::ApexResult:0x00007437b4e13218
  @column=-1,
  @compile_problem="",
  @compiled=true,
  @exception_message="",
  @exception_stack_trace="",
  @line=-1,
  @logs=
   ["61.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO",
    "Execute Anonymous: Account acc = [SELECT Id, Name FROM Account LIMIT 1];",
    "Execute Anonymous: System.debug(acc.Name);",
   ....]

For more command details, see the command reference



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sf_cli/sf/apex/run.rb', line 53

def run(target_org: nil, file: nil)
  return run_interactive(target_org) if file.nil?
  return run_by_io(target_org, file) if file.is_a? StringIO

  return unless file.is_a? String

  flags = {:"target-org" => target_org, :"file" => file}

  json = exec(__method__, flags: flags, redirection: :null_stderr)
  ApexResult.new(json['result'])
end