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

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, api_version: nil) ⇒ ApexResult

Run apex code and returns its result. If you don’t specify the script file, it starts interactive mode.

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

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);",
   ....]

Parameters:

  • target_org (Symbol, String) (defaults to: nil)

    an alias of paticular org, or username can be used.

  • file (String, #read) (defaults to: nil)

    (1) path to a local file that contains Apex code. (2) object that has #read method

  • api_version (Numeric) (defaults to: nil)

    override the api version used for api requests made by this command

Returns:

See Also:



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

def run(target_org: nil, file: nil, api_version: nil)
  _file = crate_tmpfile(file)
  path = _file&.path || file
  flags = {:"target-org" => target_org, :"file" => path, :"api-version" => api_version}

  json = exec(__method__, flags: flags, redirection: :null_stderr)
  ApexResult.new(json['result'])
ensure
  _file&.close!
end