Rails: SFDC: Extend Restforce
Restforce is a gem that facilitates usage of the Salesforce REST API. This extension provides for:
- List fields accessible to the API user for a given SFDC object
- A way to list the options for a picklist field of a given SFDC object
- A way to convert often used 16 character id key field to the 18 character id key field
List accessible fields:
>> client.fields("Account")
"**************************************************************************************************************************************"
"label name type length "
"----- ---- ---- ------ "
"Account Number AccountNumber string 40 "
"Account Source AccountSource picklist 40 "
"Active Active__c picklist 255 "
"Annual Revenue AnnualRevenue currency 0 "
"Billing City BillingCity string 40 "
"Billing Country BillingCountry string 80 "
"Billing Zip/Postal Code BillingPostalCode string 20 "
"Billing State/Province BillingState string 80 "
"Billing Street BillingStreet textarea 255 "
"Clean Status CleanStatus picklist 40 "
"Created By ID CreatedById reference 18 "
"Created Date CreatedDate datetime 0 "
"Customer Priority CustomerPriority__c picklist 255 "
"D&B Company ID DandbCompanyId reference 18 "
"Account Description Description textarea 32000 "
"D-U-N-S Number DunsNumber string 9 "
"Account Fax Fax phone 40 "
"Account ID Id id 18 "
"Industry Industry picklist 40 "
"Deleted IsDeleted boolean 0 "
"Data.com Key Jigsaw string 20 "
"Jigsaw Company ID JigsawCompanyId string 20 "
"Last Activity LastActivityDate date 0 "
"Last Modified By ID LastModifiedById reference 18 "
"Last Modified Date LastModifiedDate datetime 0 "
"Master Record ID MasterRecordId reference 18 "
"NAICS Code NaicsCode string 8 "
"NAICS Description NaicsDesc string 120 "
"Account Name Name string 255 "
"Employees NumberOfEmployees int 0 "
"Number of Locations NumberofLocations__c double 0 "
"Owner ID OwnerId reference 18 "
"Ownership Ownership picklist 40 "
"Parent Account ID ParentId reference 18 "
"PartnerPath Created Date PartnerPath_Created_Date__c datetime 0 "
"PartnerPathID PartnerPath_ID__c string 30 "
"Account Phone Phone phone 40 "
"Account Rating Rating picklist 40 "
"SLA Expiration Date SLAExpirationDate__c date 0 "
"SLA Serial Number SLASerialNumber__c string 10 "
"SLA SLA__c picklist 255 "
"Shipping City ShippingCity string 40 "
"Shipping Country ShippingCountry string 80 "
"Shipping Zip/Postal Code ShippingPostalCode string 20 "
"Shipping State/Province ShippingState string 80 "
"Shipping Street ShippingStreet textarea 255 "
"SIC Code Sic string 20 "
"SIC Description SicDesc string 80 "
"Account Site Site string 80 "
"System Modstamp SystemModstamp datetime 0 "
"Ticker Symbol TickerSymbol string 20 "
"Tradestyle Tradestyle string 255 "
"Account Type Type picklist 40 "
"Upsell Opportunity UpsellOpportunity__c picklist 255 "
"Website Website url 255 "
"Year Started YearStarted string 4 "
"**************************************************************************************************************************************"
" "
List picklist options:
>> client.picklist_options("Account","AccountSource")
"*************************************************************"
"label value "
"----- ----- "
"Web Web "
"Phone Inquiry Phone Inquiry "
"Partner Referral Partner Referral "
"Purchased List Purchased List "
"Other Other "
"*************************************************************"
" "
" " "
Convert 16 character key id to an 18 character one:
>> client.convert_guid("006f100000YvYXX")
"006f100000YvYXXAA3" "
The extension of the Restforce gem:
module Restforce
module Data
class Client < AbstractClient
#
# Public: Returns fields for an sobject.
#
def fields(sobject)
mash = self.describe(sobject)
border = "**************************************************************************************************************************************"
spaces = " "
header_1 = "#{'%-50.50s' % 'label'} #{'%-50.50s' % 'name'} #{'%-15.15s' % 'type'} #{'%-15.15s' % 'length'}"
header_2 = "#{'%-50.50s' % '-----'} #{'%-50.50s' % '----'} #{'%-15.15s' % '----'} #{'%-15.15s' % '------'}"
rows = []
table = ""
master_hash = {}
mash['fields'].each do |field|
field_hash = {}
label = field['label'].to_s.truncate(50)
name = field['name'].to_s.truncate(50)
type = field['type'].to_s.truncate(15)
length = field['length'].to_s.truncate(15)
field_hash = { label: label, name: name, type: type, length: length}
master_hash[name] = field_hash
#rows << "#{'%-50.50s' % label } #{'%-50.50s' % name } #{'%-15.15s' % type } #{'%-15.15s' % length }"
end
unless master_hash.empty?
p border
p header_1
p header_2
master_hash.sort.each do |row_hash|
p "#{'%-50.50s' % row_hash.last[:label] } #{'%-50.50s' % row_hash.last[:name] } #{'%-15.15s' % row_hash.last[:type] } #{'%-15.15s' % row_hash.last[:length].to_s }"
end
p border
p spaces
end
end
def sorted_fields(sobject)
mash = self.describe(sobject)
names =[]
mash['fields'].each do |field|
names << field['name'].to_s
end
names.sort
end
def picklist_options(sobject, picklist)
mashes = self.picklist_values(sobject, picklist)
border = "*************************************************************"
spaces = " "
header_1 = "#{'%-50.50s' % 'label'} #{'%-50.50s' % 'value'}"
header_2 = "#{'%-50.50s' % '-----'} #{'%-50.50s' % '-----'}"
rows = []
table = ""
mashes.each do |mash|
label = mash['label'].to_s.truncate(50)
value = mash['value'].to_s.truncate(50)
rows << "#{'%-50.50s' % label } #{'%-50.50s' % value }"
end
unless rows.blank?
p border
p header_1
p header_2
rows.each do |row|
p row
end
p border
p spaces
end
end
def convert_guid(short_guid)
chunks = short_guid.chars.to_a
char_map = %w{A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5}
extension = []
chunks.each_slice(5) { |x| y = x.reverse.map {|c| (c.match /[A-Z]/) ? 1 : 0 }.join("").to_i(2) ; extension << y}
short_guid + (extension.map {|e| char_map.at(e)}).join("").to_s
end
end
end
end "
Comment