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

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

Instance Method Summary collapse

Instance Method Details

#search(sosl, target_org: nil, format: nil, api_version: nil) ⇒ Hash

Note:

if you choose csv as format, csv files are downloaded in current directory

Search objects using SOSL.

Examples:

(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"]}

Parameters:

  • sosl (String)

    SOSL

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

    an alias of paticular org, or username can be used

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

    get the command’s raw output. human, csv, json can be available.

  • api_version (Numeric) (defaults to: nil)

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

Returns:

  • (Hash)

    the search result

See Also:



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

def search(sosl, target_org: nil, format: nil, api_version: nil)
  flags = {
    :"query"         => %|"#{sosl}"|,
    :"target-org"    => target_org,
    :"result-format" => format,
    :"api-version" => api_version,
  }
  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