Giter Club home page Giter Club logo

bulkforce's Introduction

Bulkforce

Build Status Coverage Status Code Climate Dependency Status Gem Version

This is a fork of the originial Executrix gem with some breaking API changes. In the long term, this gem will replace Executrix.

It is a Ruby MRI 2.1+ gem only.

Overview

Bulkforce is a simple ruby gem for connecting to and using the Salesforce Bulk API. This gem only supports the functionality provided by the bulk API.

Installation

$ sudo gem install bulkforce

How to use

After requiring using this gem is simple and straight forward.

require "bulkforce"

Authentication

The authentication is heavily inspired by Restforce

Session ID and instance

If you already have a session id and a instance you can directly authenticate against Salesforce:

salesforce = Bulkforce.new(
  session_id: "YOUR_SESSION_ID",
  instance: "YOUR_INSTANCE",
)

Username/Password/Security Token

Bulkforce supports basic authentication via username, password and security token.

salesforce = Bulkforce.new(
  username: "YOUR_SALESFORCE_USERNAME",
  password: "YOUR_SALESFORCE_PASSWORD",
  security_token: "YOUR_SALESFORCE_TOKEN,
)

OAuth

You can also authenticate via OAuth. Therefore you need the client_id, client_secret and refresh_token.

salesforce = Bulkforce.new(
  client_id: "YOUR_CLIENT_ID",
  client_secret: "YOUR_CLIENT_SECRET",
  refresh_token: "YOUR_REFRESH_TOKEN,
)

Priority

If you define credentials for multiple authentication methods, the following priority applies:

  • Session ID
  • OAuth
  • User/Password/Security Token

ENV variables

You can also define the credentials in Environment variables which are as following:

SALESFORCE_API_VERSION=...
SALESFORCE_USERNAME=...
SALESFORCE_PASSWORD=...
SALESFORCE_SECURITY_TOKEN=...
SALESFORCE_HOST=...
SALESFORCE_CLIENT_ID=...
SALESFORCE_CLIENT_SECRET=...
SALESFORCE_INSTANCE=...
SALESFORCE_REFRESH_TOKEN=...

Afterwards you can just instantiate the client:

Bulkforce.new

Sandbox

To use the bulkforce against your salesforce sandbox, change the host to test.salesforce.com

salesforce = Bulkforce.new(
  # credentials
  host: "test.salesforce.com",
)

OrgId

After you created the client object you can fetch the OrgId via org_id.

This will fetch the 15 digit OrgId.

salesforce.org_id # "00D50000000IehZ"

Operations

# Insert
new_account = {"name" => "Test Account", "type" => "Other"} # Add as many fields per record as needed.
records_to_insert = []
records_to_insert << new_account # You can add as many records as you want here, just keep in mind that Salesforce has governor limits.
result = salesforce.insert("Account", records_to_insert)
puts "reference to the bulk job: #{result.inspect}"
# Update
updated_account = {"name" => "Test Account -- Updated", "id" => "a00A0001009zA2m"} # Nearly identical to an insert, but we need to pass the salesforce id.
records_to_update = []
records_to_update.push(updated_account)
salesforce.update("Account", records_to_update)
# Upsert
upserted_account = {"name" => "Test Account -- Upserted", "External_Field_Name" => "123456"} # Fields to be updated. External field must be included
records_to_upsert = []
records_to_upsert.push(upserted_account)
salesforce.upsert("Account", records_to_upsert, "External_Field_Name") # Note that upsert accepts an extra parameter for the external field name
# Delete
deleted_account = {"id" => "a00A0001009zA2m"} # We only specify the id of the records to delete
records_to_delete = []
records_to_delete.push(deleted_account)
salesforce.delete("Account", records_to_delete)
# Query
res = salesforce.query("Account", "select id, name, createddate from Account limit 3") # We just need to pass the sobject name and the query string
puts res.result.records.inspect

File Upload

For file uploads, just add a File object to the binary columns.

attachment = {"ParentId" => "00Kk0001908kqkDEAQ", "Name" => "attachment.pdf", "Body" => File.new("tmp/attachment.pdf")}
records_to_insert = []
records_to_insert << attachment
salesforce.insert("Attachment", records_to_insert)

Query status

The above examples all return immediately after sending the data to the Bulk API. If you want to wait, until the batch finished, call the final_status method on the batch-reference.

new_account = {"name" => "Test Account", "type" => "Other"} # Add as many fields per record as needed.
records_to_insert = []
records_to_insert << new_account # You can add as many records as you want here, just keep in mind that Salesforce has governor limits.
batch_reference = salesforce.insert("Account", records_to_insert)
results = batch_reference.final_status
puts "the results: #{results.inspect}"

Additionally you cann pass in a block to query the current state of the batch job:

new_account = {"name" => "Test Account", "type" => "Other"} # Add as many fields per record as needed.
records_to_insert = []
records_to_insert << new_account # You can add as many records as you want here, just keep in mind that Salesforce has governor limits.
batch_reference = salesforce.insert("Account", records_to_insert)
results = batch_reference.final_status do |status|
  puts "running: #{status.inspect}"
end
puts "the results: #{results.inspect}"

The block will yield every 2 seconds, but you can also specify the poll interval:

new_account = {"name" => "Test Account", "type" => "Other"} # Add as many fields per record as needed.
records_to_insert = []
records_to_insert << new_account # You can add as many records as you want here, just keep in mind that Salesforce has governor limits.
batch_reference = salesforce.insert("Account", records_to_insert)
poll_interval = 10
results = batch_reference.final_status(poll_interval) do |status|
  puts "running: #{status.inspect}"
end
puts "the results: #{results.inspect}"

Copyright

Copyright (c) 2012 Jorge Valdivia. Copyright (c) 2015 Leif Gensert, Propertybase GmbH

bulkforce's People

Contributors

leifg avatar

Stargazers

 avatar

Watchers

Michael Wenglein avatar Julius Obermaier avatar Thomas Rudolf avatar Peter Siegl avatar Ben Bieker avatar James Cloos avatar Johannes Klose avatar Jeremy Whiteley avatar Richard Grossman avatar  avatar Andreas Lengkeek avatar Bastien BARBÉ avatar  avatar

Forkers

hooptie45 leifg

bulkforce's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.