module SoqlObjectDescribe
Methods relating to describing a soql object See developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_sobject_describe.htm for reference. Note the data here is cached. If metadata changes you would need to re run Ruby Code to get updates if a call has already been made
Public Instance Methods
@return [Hash] List of accessors for an object and what they relate to
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 103 def accessors return @attr_hash if @attr_hash @attr_hash = {} fields.each do |field| @attr_hash[field['label'].unused_ruby_name.to_sym] = important_attributes_for(field) end @attr_hash end
Reference developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/sobject_describe_with_ifmodified_header.htm @todo Get this to work
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 11 def changes_from_date(_date) @changes_from_date ||= new("describe #{self}", method: :get, suburl: "sobjects/#{soql_object_name}/describe/", params: { if_modified_since: 'Wed, 3 Jul 2013 19:43:31 GMT' }) end
@param [String, Symbol] field_name Salesforce
backend field name @return [String, nil] Default value for field provided
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 66 def default_for(field_name) properties_for(field_name)['defaultValue'] end
@return [SoqlData] Retrieve JSON that describes current object
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 17 def description @description ||= new("describe #{self}", method: :get, suburl: "sobjects/#{soql_object_name}/describe/") end
@return [Array] Field values for field names
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 98 def field_names fields.collect { |field| field['name'] } end
@return [Array] List of fields that each are a hash of attributes
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 32 def fields @fields ||= description[:fields] end
@return [Array] Label names of object
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 92 def label_names fields.collect { |field| field['label'] } end
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 21 def layouts @layouts ||= new("layouts for #{self}", method: :get, suburl: "sobjects/#{soql_object_name}/describe/layouts") end
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 26 def layouts_for(record_type_id) new("layouts for #{self} on #{record_type_id}", method: :get, suburl: "sobjects/#{soql_object_name}/describe/layouts/#{record_type_id}") end
@param [String, Symbol] field_name Salesforce
backend field name @return [Integer] Max length of field
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 60 def length_for(field_name) properties_for(field_name)['length'] end
Finding Picklist values for specified fields @param [String, Symbol] field_name Salesforce
backend field name @return [Array] List of values for passed in field_name
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 52 def picklist_for(field_name) properties = properties_for field_name properties['picklistValues'].collect { |list| list['label'] }.compact end
@return [Array] List of labels that have picklists
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 75 def picklists label_names.find_all { |f| type_for(f) == 'picklist' } end
@param [String, Symbol] field_name Salesforce
backend field name @return [Hash] Hash storing all properties of a field
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 38 def properties_for(field_name) field_name = field_name.to_s properties = fields.find { |field| %w[name label].any? { |label| field[label] == field_name } } unless properties raise LeapSalesforce::ResponseError, "Field name '#{field_name}' not found in '#{self}'" \ " using table name '#{soql_object_name}'. Field names are #{field_names}" end properties end
@param [String, Symbol] field_name Salesforce
backend field name @return [String, nil] Other entity this field relates to if it's a reference field
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 87 def relationship_name_for(field_name) properties_for(field_name)['relationshipName'] end
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 70 def required fields.find_all { |field| field[''] } end
@param [String, Symbol] field_name Salesforce
backend field name @return [String] Type of field (e.g., picklist, string, double, reference)
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 81 def type_for(field_name) properties_for(field_name)['type'] end
Private Instance Methods
@return [Hash] Important attributes for a field
# File lib/leap_salesforce/soql_data/soql_object_describe.rb, line 116 def important_attributes_for(field) important_attributes = { backend: field['name'], label: field['label'], type: field['type'] } relationship_name = field['relationshipName'] important_attributes[:related_object] = relationship_name if relationship_name important_attributes end