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
-
#ids_to_delete ⇒ Hash
List of ids to delete when deleting a particular object.
Instance Method Summary collapse
-
#any?(lookup = {}) ⇒ Boolean
(also: #any_where?)
Whether any result for lookup.
-
#delete(id, must_pass: false) ⇒ self
Remove object from Salesforce with provided id.
-
#delete_ids_with(lookup) ⇒ Object
Remove all ids from table that match lookup criteria.
-
#find(lookup = {}) ⇒ < SoqlData] Instance of itself storing reference to found object
Find the data for a single SoqlObject using the calling class’s table.
- #get(lookup) ⇒ < SoqlData] Exchange with details of data deprecated Deprecated.
-
#id_where(lookup) ⇒ String
Id that matches filter.
-
#ids_where(lookup = {}) {|id| ... } ⇒ Array
(also: #each_id_with)
Perform the code in the block for all the ids matching a query.
-
#list(lookup = {}) ⇒ Array
Return list of objects (matching criteria if passed).
-
#remove_dependent_records(_id) ⇒ Object
Override to handle removing dependent records.
-
#soql ⇒ LeapSalesforce::Soql
Object to handle Soql interactions.
-
#soql_element(name, backend_name) ⇒ Object
Define a mapping between a ruby friendly name to reference field and Salesforce backend name.
-
#update(id, data) ⇒ self
Remove object from Salesforce with provided id.
-
#where(lookup) ⇒ Array
(also: #each_with)
List of Soql objects matching criteria.
Methods included from LeapSalesforce::Tooling
#run_test_asynchronous, #tooling_objects
Instance Attribute Details
#ids_to_delete ⇒ Hash
Returns 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.
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
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
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.
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..include?('"totalSize":0') raise LeapSalesforce::RequestError, "Could not find #{self} using #{lookup}" end end |
#get(lookup) ⇒ < SoqlData] Exchange with details of data
Get details of itself by searching for it’s id Store response within itself
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.
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
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)
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
17 |
# File 'lib/leap_salesforce/soql_data/soql_global_object_data.rb', line 17 def remove_dependent_records(_id); end |
#soql ⇒ LeapSalesforce::Soql
Returns Object to handle Soql interactions.
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
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
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.
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 |