Module: SoqlGlobalObjectData

Includes:
LeapSalesforce::Tooling
Included in:
SoqlData
Defined in:
lib/leap_salesforce/soql_data/soql_global_object_data.rb

Overview

Methods for working with instances of global to soql objects, not global overall Some of these are called internally by methods on an instance of SoqlData, for instance, case = Case.create case.update # => This will call Case.update passing it’s own id See developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_sobject_describe.htm

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LeapSalesforce::Tooling

#run_test_asynchronous, #tooling_objects

Instance Attribute Details

#ids_to_deleteHash

Returns List of ids to delete when deleting a particular object.

Returns:

  • (Hash)

    List of ids to delete when deleting a particular object



13
14
15
# File 'lib/leap_salesforce/soql_data/soql_global_object_data.rb', line 13

def ids_to_delete
  @ids_to_delete
end

Instance Method Details

#any?(lookup = {}) ⇒ Boolean Also known as: any_where?

Returns Whether any result for lookup.

Returns:

  • (Boolean)

    Whether any result for lookup



64
65
66
# File 'lib/leap_salesforce/soql_data/soql_global_object_data.rb', line 64

def any?(lookup = {})
  soql.lookup_id(lookup)[:totalSize] != 0
end

#delete(id, must_pass: false) ⇒ self

Remove object from Salesforce with provided id

Examples:

Delete a contact with a specified id, failing if the delete fails

Contact.delete '0032v00002rgv2pAAA', must_pass: true

Parameters:

  • id (String, Symbol)

    Id of element to remove

  • must_pass (Boolean) (defaults to: false)

    Whether to raise exception if call is not successful

Returns:

  • (self)

    Exchange object making delete call



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/leap_salesforce/soql_data/soql_global_object_data.rb', line 139

def delete(id, must_pass: false)
  enforce_type_for id
  SoqlData.ids_to_delete.reject! { |table, id_to_remove| table == self && id_to_remove == id } # Remove id from list to delete
  remove_dependent_records(id)

  SoqlHandler.new("Delete #{id}").use
  delete = new("SOQL Delete #{id}", method: :delete, suburl: "sobjects/#{soql_object_name}/#{id}")
  delete.call
  return delete unless must_pass

  delete.successful?
  delete
end

#delete_ids_with(lookup) ⇒ Object

Remove all ids from table that match lookup criteria

Parameters:

  • lookup (Hash)

    Key value pair unique to Salesforce to query for



107
108
109
# File 'lib/leap_salesforce/soql_data/soql_global_object_data.rb', line 107

def delete_ids_with(lookup)
  each_id_with(lookup, &method(:delete))
end

#find(lookup = {}) ⇒ < SoqlData] Instance of itself storing reference to found object

Find the data for a single SoqlObject using the calling class’s table. Will get the latest created date.

Examples:

Get any contact

Contact.find

Get a contact where LastName is ‘Bob’ using backend name

Contact.find(LastName: 'Bob')

Get a contact that includes ‘Test’ in their first name (using ruby accessor name)

Contact.find(first_name: '~%Test%')

Get a contact created 10 days ago

Contact.find CreatedDate: "<#{10.days.ago}"

Parameters:

  • lookup (Hash) (defaults to: {})

    Key value pair unique to Salesforce to query for

Options Hash (lookup):

  • :teardown (Boolean)

    Whether to remove id after scenario finished

Returns:

  • (< SoqlData] Instance of itself storing reference to found object)

    < SoqlData] Instance of itself storing reference to found object



32
33
34
35
36
37
38
39
40
41
# File 'lib/leap_salesforce/soql_data/soql_global_object_data.rb', line 32

def find(lookup = {})
  id_lookup = soql.lookup_id lookup
  begin
    lookup.key?(:Id) ? id_lookup : soql.data_from_url(id_lookup['$..url'], lookup)
  rescue NoElementAtPath => e
    raise e unless e.message.include?('"totalSize":0')

    raise LeapSalesforce::RequestError, "Could not find #{self} using #{lookup}"
  end
end

#get(lookup) ⇒ < SoqlData] Exchange with details of data

Deprecated.

Get details of itself by searching for it’s id Store response within itself

Returns:

  • (< SoqlData] Exchange with details of data)

    < SoqlData] Exchange with details of data



52
53
54
55
56
# File 'lib/leap_salesforce/soql_data/soql_global_object_data.rb', line 52

def get(lookup)
  LeapSalesforce.logger.warn "Method 'get' called when it is deprecated" \
  " from #{caller_locations[0]}"
  find(lookup)
end

#id_where(lookup) ⇒ String

Returns Id that matches filter.

Returns:

  • (String)

    Id that matches filter



59
60
61
# File 'lib/leap_salesforce/soql_data/soql_global_object_data.rb', line 59

def id_where(lookup)
  soql.lookup_id(lookup).id
end

#ids_where(lookup = {}) {|id| ... } ⇒ Array Also known as: each_id_with

Perform the code in the block for all the ids matching a query. If no block, return a list of objects

Parameters:

  • lookup (Hash) (defaults to: {})

    Key value pair unique to Salesforce to query for

Yields:

  • (id)

    Perform block for each id returned. The ‘id’ parameter in a block represents an id matching the query

Returns:

  • (Array)

    List of ids matching criteria. Only used if no block given



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/leap_salesforce/soql_data/soql_global_object_data.rb', line 75

def ids_where(lookup = {}, &block)
  lookup[:limit] ||= nil # Don't limit results returned
  SoqlHandler.new("Each Id where #{self}").use
  results = soql.query soql.soql_id(lookup), wait: false
  ids = results.ids
  if block_given?
    ids.each(&block)
  else
    ids
  end
end

#list(lookup = {}) ⇒ Array

Return list of objects (matching criteria if passed)

Parameters:

  • lookup (Hash) (defaults to: {})

    Key value pair unique to Salesforce to query for. Leave empty for all

Returns:

  • (Array)

    List of all soql objects



101
102
103
# File 'lib/leap_salesforce/soql_data/soql_global_object_data.rb', line 101

def list(lookup = {})
  where(lookup)
end

#remove_dependent_records(_id) ⇒ Object

Override to handle removing dependent records

Parameters:

  • _id (String)

    Id of record being deleted



17
# File 'lib/leap_salesforce/soql_data/soql_global_object_data.rb', line 17

def remove_dependent_records(_id); end

#soqlLeapSalesforce::Soql

Returns Object to handle Soql interactions.

Returns:



44
45
46
# File 'lib/leap_salesforce/soql_data/soql_global_object_data.rb', line 44

def soql
  @soql ||= LeapSalesforce::Soql.new(self)
end

#soql_element(name, backend_name) ⇒ Object

Define a mapping between a ruby friendly name to reference field and Salesforce backend name

Parameters:

  • name (Symbol, String)

    Name of accessor to refer to field/element with

  • backend_name (String)

    Name of Salesforce field



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/leap_salesforce/soql_data/soql_global_object_data.rb', line 156

def soql_element(name, backend_name)
  if String::SOQLDATA_METHODS.include?(name.to_s)
    LeapSalesforce.logger.warn "Cannot create metadata for '#{name}' (defined at #{caller_locations[0]})" \
    ' as method already defined in SoqlData'
    return
  end

  # Either set the element (if creating a new record) or update the object
  # @param [String] new_value Value to update record to
  define_method("#{name}=") do |new_value|
    if @response # Performing an update
      @response = update(backend_name => new_value).response
    else # Setting value on creating new object. Uses id of object if SoqlData object passed
      self[backend_name] = new_value.class < SoqlData ? new_value.id : new_value
    end
  end

  # @return [String] Value of backend name
  define_method(name.to_s) { self[backend_name] }
  # @return [String] Name of backend name for element
  define_method("#{name}_element") { backend_name }
end

#update(id, data) ⇒ self

Remove object from Salesforce with provided id

Parameters:

  • id (String)

    Id of element to update

  • data (Hash)

    Key value pairs with data to update

Returns:

  • (self)

    SoqlData object representing result of API update call



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/leap_salesforce/soql_data/soql_global_object_data.rb', line 115

def update(id, data)
  enforce_type_for id
  must_pass = data.delete(:must_pass)
  data = data.transform_values do |value|
    value.is_a?(Time) ? value.salesforce_format : value
  end
  data.transform_keys! { |key| soql.map_key(key) } # Map keys to valid field names
  SoqlHandler.new("Update #{id}").use
  update = new("Update #{self}, #{id} with '#{data}'", method: :patch,
                                                       suburl: "sobjects/#{soql_object_name}/#{id}", body: data)
  update.call
  return update unless must_pass

  update.successful?
  update
end

#where(lookup) ⇒ Array Also known as: each_with

Returns List of Soql objects matching criteria.

Parameters:

  • lookup (Hash)

    Key value pair unique to Salesforce to query for

Returns:

  • (Array)

    List of Soql objects matching criteria



91
92
93
94
# File 'lib/leap_salesforce/soql_data/soql_global_object_data.rb', line 91

def where(lookup)
  ids = each_id_with lookup
  ids.collect { |id| find(Id: id) }
end