Class: SfCli::Sf::Data::Core

Inherits:
Object
  • Object
show all
Includes:
Core::Base, HelperMethods
Defined in:
lib/sf_cli/sf/data/core.rb

Overview

Instance Attribute Summary

Attributes included from Core::Base

#varbose

Instance Method Summary collapse

Instance Method Details

#create_record(object_type, values: {}, target_org: nil) ⇒ Object

create a object record. (eqivalent to sf data create record)

object_type — Object Type (ex. Account)
values — field values to be assigned
target_org — an alias of paticular org, not default one

examples

sf.data.create_record :TheCustomObject__c, values: {Name: "John Smith", Age: 33} # creating a TheCustomObject record with name and age

For more command details, see the command reference



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/sf_cli/sf/data/core.rb', line 137

def create_record(object_type, values: {}, target_org: nil)
  field_values = field_value_pairs(values)
  flags = {
    :"sobject"    => object_type,
    :"values"      => (field_values.nil? ? nil : %|"#{field_values}"|),
    :"target-org" => target_org,
  }
  action = __method__.to_s.tr('_', ' ')
  json = exec(action, flags: flags, redirection: :null_stderr)

  json['result']['id']
end

#delete_record(object_type, record_id: nil, where: nil, target_org: nil) ⇒ Object

delete a object record. (eqivalent to sf data delete record)

object_type — Object Type (ex. Account)
record_id — id of the object
where — hash object that is used to identify a record
target_org — an alias of paticular org, not default one

examples

sf.data.delete_record :Hoge__c, record_id: 'xxxxxxx'
sf.data.delete_record :Hoge__c, where: {Name: 'Jonny B.Good', Country: 'USA'}

For more command details, see the command reference



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/sf_cli/sf/data/core.rb', line 163

def delete_record(object_type, record_id: nil, where: nil, target_org: nil)
  where_conditions = field_value_pairs(where)
  flags = {
    :"sobject"    => object_type,
    :"record-id"  => record_id,
    :"where"      => (where_conditions.nil? ? nil : %|"#{where_conditions}"|),
    :"target-org" => target_org,
  }
  action = __method__.to_s.tr('_', ' ')
  json = exec(action, flags: flags, redirection: :null_stderr)

  json['result']['id']
end

#get_record(object_type, record_id: nil, where: nil, target_org: nil, model_class: nil) ⇒ Object

get a object record. (eqivalent to sf data get record)

object_type — Object Type (ex. Account)
record_id — id of the object
where — hash object that is used to identify a record
target_org — an alias of paticular org, not default one
model_class — the object model class

examples

sf.data.get_record :Account, record_id: 'xxxxxxx'
sf.data.get_record :Account, where: {Name: 'Jonny B.Good', Country: 'USA'}

CustomObject = Struct.new(:Id, :Name)
sf.data.get_record :TheCustomObject__c, record_id: 'xxxxx', model_class: CustomObject  # returns a CustomObject struct object

For more command details, see the command reference



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sf_cli/sf/data/core.rb', line 78

def get_record(object_type, record_id: nil, where: nil, target_org: nil, model_class: nil)
  where_conditions = field_value_pairs(where)
  flags = {
    :"sobject"    => object_type,
    :"record-id"  => record_id,
    :"where"      => (where_conditions.nil? ? nil : %|"#{where_conditions}"|),
    :"target-org" => target_org,
  }
  action = __method__.to_s.tr('_', ' ')
  json = exec(action, flags: flags, redirection: :null_stderr)

  result = json['result']
  result.delete 'attributes'

  model_class ? model_class.new(**result) : result
end

#query(soql, target_org: nil, format: nil, model_class: nil) ⇒ Object

get object records using SQOL. (eqivalent to sf data query)

soql — SOQL
target_org — an alias of paticular org, not default one
model_class — the object model class

examples

sf.data.query 'SELECT Id, Name FROM Account LIMIT 1' # => [{Id: "abc", Name: "account name"}]

Account = Struct.new(:Id, :Name)
sf.data.query('SELECT Id, Name From Account Limit 3', model_class: Account)  # returns an array of Account struct object

# child-parent relationship is supported
sf.data.query 'SELECT Id, Name, Account.Name From Contact Limit 1'  #  [{Id: "abc", Name: "contact name", Account: {Name: "account name"}}]

# parent-children relationship is supported
sf.data.query 'SELECT Id, Name, (SELECT Name From Contacts) FROM Account Limit 1'  #  [{Id: "abc", Name: "account name", Contacts: [{Name: "contact name"}]}]

Account = Struct.new(:Id, :Name) # you can manually prepare the object model
sf.data.query('SELECT Id, Name From Account Limit 3', model_class: Account)  # returns an array of Account

For more command details, see the command reference

About querying with auto generated object model, see the section “Object Model support” in README.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sf_cli/sf/data/core.rb', line 42

def query(soql, target_org: nil, format: nil, model_class: nil)
  flags    = {
    :"query"    => %("#{soql}"),
    :"target-org" => target_org,
    :"result-format" => format,
  }

  raw_output = format ? true : false
  format = format || :json

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

  result['result']['records'].each_with_object([]) do |h, a|
    record = prepare_record(h)
    a << (model_class ? model_class.new(**record) : record)
  end
end

#update_record(object_type, record_id: nil, where: nil, values: nil, target_org: nil) ⇒ Object

update a object record. (eqivalent to sf data update record)

object_type — Object Type (ex. Account)
record_id — id of the object
where — field values that is used to identify a record
values — field values for update
target_org — an alias of paticular org, not default one

examples

sf.data.update_record :Account, record_id: 'xxxxxxx', values: {Name: 'New Account Name'}
sf.data.update_record :Hoge__c, where: {Name: 'Jonny B.Good', Country: 'USA'}, values: {Phone: 'xxxxx', bar: 2000}

For more command details, see the command reference



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/sf_cli/sf/data/core.rb', line 109

def update_record(object_type, record_id: nil, where: nil, values: nil, target_org: nil)
  where_conditions  = field_value_pairs(where)
  field_values      = field_value_pairs(values)
  flags = {
    :"sobject"    => object_type,
    :"record-id"  => record_id,
    :"where"      => (where_conditions.nil? ? nil : %|"#{where_conditions}"|),
    :"values"     => (field_values.nil? ? nil : %|"#{field_values}"|),
    :"target-org" => target_org,
  }
  action = __method__.to_s.tr('_', ' ')
  json = exec(action, flags: flags, redirection: :null_stderr)

  json['result']['id']
end