Module: SfCli::Sf::Data::Search

Included in:
Core
Defined in:
lib/sf_cli/sf/data/search.rb

Instance Method Summary collapse

Instance Method Details

#search(sosl, target_org: nil, format: nil) ⇒ Object

search objects using SOSL.

sosl — SOSL

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

format — get the command’s raw output. human, csv, json can be available. NOTE: if you choose csv, csv files are downloaded in current directory

# example (in irb):

> sf.data.search "FIND {TIM OR YOUNG OR OIL} IN Name Fields"
=>
{"Lead"=>["00Q5j00000WgEuDEAV"],
 "Account"=>["0015j00001U2XvNAAV", "0015j00001U2XvMAAV", "0015j00001U2XvJAAV"],
 "Contact"=>
  ["0035j00001HB84BAAT",
   "0035j00001HB84DAAT"],
 "Opportunity"=>
  ["0065j00001XHJLjAAP",
   "0065j00001XHJLTAA5",
   "0065j00001XHJLJAA5"],
 "User"=>["0055j00000CcL2bAAF", "0055j00000CcL1YAAV"]}

For more command details, see the command reference



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sf_cli/sf/data/search.rb', line 29

def search(sosl, target_org: nil, format: nil)
  flags = {
    :"query"         => %|"#{sosl}"|,
    :"target-org"    => target_org,
    :"result-format" => format,
  }
  raw_output = format ? true : false
  format = format&.to_sym || :json

  result = exec(__method__, flags: flags, redirection: :null_stderr, raw_output: raw_output, format: format)

  return if format == :csv
  return result if format == :human

  result['result']['searchRecords']
    .group_by{|r| r['attributes']['type']}
    .each_with_object({}) do |(object_type, records), result|
      result[object_type] = records.map{|r| r['Id']}
    end
end