Salesforce CLI library

This is a class library for introducing Salesforce CLI to Ruby scripting.
It is designed to be similar usability to the original command.
Currently only sf command is the target of development.

prerequisite

Salesforce CLI must be installed.
As of as of August in 2024, ver.2.54.6 is the development target.

install

Rubygem

$ gem install sf_cli

Bundler

in Gemfile:

gem 'sf_cli'

then,

$ bundle install

Examples

Since 0.0.4

require 'sf_cli'
#
# login to org
#
sf.org.
sf.org. target_org: :dev  # if the org you login isn't the default one, you should give it alias name for later use. 

#
# get records
#
sf.data.query 'SELECT Id, Name FROM Account LIMIT 1' # => [{Id: "abc", Name: "account 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

# 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"}]}]

#
# get sobject schema
#
sf.sobject.describe :Case  # get Case Object schema information

#
# generate a Salesforce DX project with manifest generation option
#
sf.project.generate 'MyProject', manifest: true

Before 0.0.3

require 'sf_cli/sf'

sf = SfCli::Sf.new

# login to org
sf.org.
sf.org. target_org: :dev  # name an alias to the org, which you're going to log in, for later use. This is needed when you don't use the default org.

# get Account records
sf.data.query 'SELECT Id, Name FROM Account LIMIT 3' # => returns an array containing 3 records

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

# generate a Salesforce DX project
sf.project.generate 'MyProject'

Object Model support (experimental, since 0.0.4)

require 'sf_cli'
require 'sf_cli/sf/model'

# generates Model Class for Contact and Account, accessing the org aliased as 'dev'
SfCli::Sf::Model.generate %w[Contact Account], target_org: :dev

c = Contact.new(:Name => "John Smith")
c.Name # => "John Smith"

rows = sf.data.query 'SELECT Id, Name, Account.Name From Contact Limit 1', model_class: Contact
rows.size # => 1
rows.first.Name # => Name of the Contact
rows.first.Account.Name # => Name of the Account

Documents

The following steps generate doc directory, which all documents are generated in.

$ git clone https://github.com/tmkw/sf_cli.git
$ cd sf_cli
$ bundle install
$ bundle exec rake rdoc

Or, you can read the same documents online at rubygems.org