Giter Club home page Giter Club logo

salesforce_bulk's Introduction

salesforce-bulk

Overview

Salesforce bulk is a simple ruby gem for connecting to and using the Salesforce Bulk API (www.salesforce.com/us/developer/docs/api_asynch/index.htm). There are already some gems out there that provide connectivity to the Salesforce SOAP and Rest APIs, if your needs are simple, I recommend using those, specifically raygao’s asf-rest-adapter (github.com/raygao/asf-rest-adapter) or databasedotcom (rubygems.org/gems/databasedotcom).

Installation

sudo gem install salesforce_bulk

How to use

Using this gem is simple and straight forward.

To initialize:

require 'salesforce_bulk'
salesforce = SalesforceBulk::Api.new("YOUR_SALESFORCE_USERNAME", "YOUR_SALESFORCE_PASSWORD+YOUR_SALESFORCE_TOKEN")

To use sandbox:

salesforce = SalesforceBulk::Api.new("YOUR_SALESFORCE_SANDBOX_USERNAME", "YOUR_SALESFORCE_PASSWORD+YOUR_SALESFORCE_SANDBOX_TOKEN", true)

Note: the second parameter is a combination of your Salesforce token and password. So if your password is xxxx and your token is yyyy, the second parameter will be xxxxyyyy

Sample operations:

# Insert/Create
new_account = Hash["name" => "Test Account", "type" => "Other"] # Add as many fields per record as needed.
records_to_insert = Array.new
records_to_insert.push(new_account) # You can add as many records as you want here, just keep in mind that Salesforce has governor limits.
result = salesforce.create("Account", records_to_insert)
puts "result is: #{result.inspect}"

# Update
updated_account = Hash["name" => "Test Account -- Updated", "id" => "a00A0001009zA2m"] # Nearly identical to an insert, but we need to pass the salesforce id.
records_to_update = Array.new
records_to_update.push(updated_account)
salesforce.update("Account", records_to_update)

# Upsert
upserted_account = Hash["name" => "Test Account -- Upserted", "External_Field_Name" => "123456"] # Fields to be updated. External field must be included
records_to_upsert = Array.new
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

OR

salesforce.upsert("Account", records_to_upsert, "External_Field_Name", true) # last parameter indicates whether to wait until the batch finishes

# Delete
deleted_account = Hash["id" => "a00A0001009zA2m"] # We only specify the id of the records to delete
records_to_delete = Array.new
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

Result reporting:

new_account = Hash["type" => "Other"] # Missing required field "name."
records_to_insert = Array.new
records_to_insert.push(new_account) # You can add as many records as you want here, just keep in mind that Salesforce has governor limits.
result = salesforce.create("Account", records_to_insert, true) # Result reporting can only be used if wait is set to true
puts result.success? # false
puts result.has_errors? # true
puts result.result.errors # An indexed hash detailing the errors

Copyright © 2012 Jorge Valdivia.

end

salesforce_bulk's People

Contributors

javierjulio avatar jorgevaldivia avatar ramonmorakms avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

salesforce_bulk's Issues

Errors when parsing returned results

I am trying to use the salesforce_bulk gem to query our Salesforce instance. I can log in successfully, but when I query I get the following results, or similar:

res = salesforce.query("Account", "SELECT id, name FROM Account LIMIT 1")
CSV::IllegalFormatError: CSV::IllegalFormatError
    from /Users/terry/.rbenv/versions/1.8.7-p358/lib/ruby/1.8/csv.rb:607:in `get_row'
    from /Users/terry/.rbenv/versions/1.8.7-p358/lib/ruby/1.8/csv.rb:556:in `each'
    from /Users/terry/.rbenv/versions/1.8.7-p358/lib/ruby/1.8/csv.rb:127:in `collect'
    from /Users/terry/.rbenv/versions/1.8.7-p358/lib/ruby/1.8/csv.rb:127:in `parse'
    from /Users/terry/.rbenv/versions/1.8.7-p358/gemsets/pcwi-sf-services/gems/salesforce_bulk-1.0.0/lib/salesforce_bulk/job.rb:132:in `parse_results'
    from /Users/terry/.rbenv/versions/1.8.7-p358/gemsets/pcwi-sf-services/gems/salesforce_bulk-1.0.0/lib/salesforce_bulk/job.rb:123:in `get_batch_result'
    from /Users/terry/.rbenv/versions/1.8.7-p358/gemsets/pcwi-sf-services/gems/salesforce_bulk-1.0.0/lib/salesforce_bulk.rb:60:in `do_operation'
    from /Users/terry/.rbenv/versions/1.8.7-p358/gemsets/pcwi-sf-services/gems/salesforce_bulk-1.0.0/lib/salesforce_bulk.rb:35:in `query'
    from (irb):2

Or this:

res = salesforce.query("Account", "SELECT id FROM Account LIMIT 1")
TypeError: can't convert String into Integer
    from /Users/terry/.rbenv/versions/1.8.7-p358/gemsets/pcwi-sf-services/gems/salesforce_bulk-1.0.0/lib/salesforce_bulk/job.rb:141:in `[]'
    from /Users/terry/.rbenv/versions/1.8.7-p358/gemsets/pcwi-sf-services/gems/salesforce_bulk-1.0.0/lib/salesforce_bulk/job.rb:141:in `parse_results'
    from /Users/terry/.rbenv/versions/1.8.7-p358/gemsets/pcwi-sf-services/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:182:in `each_with_index'
    from /Users/terry/.rbenv/versions/1.8.7-p358/gemsets/pcwi-sf-services/gems/salesforce_bulk-1.0.0/lib/salesforce_bulk/job.rb:134:in `each'
    from /Users/terry/.rbenv/versions/1.8.7-p358/gemsets/pcwi-sf-services/gems/salesforce_bulk-1.0.0/lib/salesforce_bulk/job.rb:134:in `each_with_index'
    from /Users/terry/.rbenv/versions/1.8.7-p358/gemsets/pcwi-sf-services/gems/salesforce_bulk-1.0.0/lib/salesforce_bulk/job.rb:134:in `parse_results'
    from /Users/terry/.rbenv/versions/1.8.7-p358/gemsets/pcwi-sf-services/gems/salesforce_bulk-1.0.0/lib/salesforce_bulk/job.rb:123:in `get_batch_result'
    from /Users/terry/.rbenv/versions/1.8.7-p358/gemsets/pcwi-sf-services/gems/salesforce_bulk-1.0.0/lib/salesforce_bulk.rb:60:in `do_operation'
    from /Users/terry/.rbenv/versions/1.8.7-p358/gemsets/pcwi-sf-services/gems/salesforce_bulk-1.0.0/lib/salesforce_bulk.rb:35:in `query'
    from (irb):3

Any help on resolving these would be greatly appreciated.

Thanks,

TR

Invalid Login

I am having an issue authenticating.

salesforce = SalesforceBulk::Api.new("[email protected]", "PasswordSecurityToken", true)

and the output is

RuntimeError: INVALID_LOGIN: Invalid username, password, security token; or user locked out.

I am currently running the databasedotcom and restforce gem's and authenticating using to my sandbox and production without issue using the same credentials I am using for salesforce_bulk. Am I missing something obvious?

Oauth2 token for this gem

If i am using Oauth2 for authentication.i can access the access token . so how shud i use that to use this gem rather tan the username and password

Job Id

Is it possible to get the job id from the results? We would like to poll the job after it has bee submitted.

Thanks

Broken in Ruby 2

We recently tried to upgrade to Ruby 2 (2.0 or 2.1) and found that it broke the salesforce_bulk gem. In particular an error already gets raised trying to login.

NoMethodError: undefined method `attributes' for nil:NilClass

Backtrace:

["/opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/xml-simple-1.1.3/lib/xmlsimple.rb:714:in get_attributes'", "/opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/xml-simple-1.1.3/lib/xmlsimple.rb:461:incollapse'", "/opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/xml-simple-1.1.3/lib/xmlsimple.rb:194:in xml_in'", "/opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/xml-simple-1.1.3/lib/xmlsimple.rb:203:inxml_in'", "/opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/bundler/gems/salesforce_bulk-c2de042a57a2/lib/salesforce_bulk/connection.rb:101:in parse_response'", "/opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/bundler/gems/salesforce_bulk-c2de042a57a2/lib/salesforce_bulk/connection.rb:45:inlogin'", "/opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/bundler/gems/salesforce_bulk-c2de042a57a2/lib/salesforce_bulk/connection.rb:21:in initialize'", "/opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/bundler/gems/salesforce_bulk-c2de042a57a2/lib/salesforce_bulk.rb:15:innew'", "/opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/bundler/gems/salesforce_bulk-c2de042a57a2/lib/salesforce_bulk.rb:15:in `initialize'"]

It looks like some issue with encodings in XmlSimple if I see this right.

I tried to add some custom logging in a fork to print the response and the result was some really screwed up string.

The error I raise should have looked like: "Error trying to parse the following response from Salesforce: #{response} - Error details: #{error.class.name}: #{error.message}. ..."

Instead the output I see in the console was like:

Error trying to parse the following response from Salesforce: �U�r�:������
�p%N��H�b.�g~3z�s>lMA��\�P)��J�~m8�,K���ע�*�D��9�r��Z�S�3    �-�f�4������=o6�t�{^M'Q�-�&���s��\�;�&U��L.2��hL3��eϵ ccH�
1YPa�S36T�j���Ol�Gz�+�|Y����8&'����i�5<�����ݫ����{����l�ݳ[���S8�p��>�y�ɽRt����������I�>��?�Mf�ɛ{>�D�"�&6�mJe�:���x�7�j��ȅ"�2V(���n�bh�%�Y����]�K� o0:�`;BA�Q�J@f#. �O0�  �H����8�~
                                                                                jg��<6>�����Tr�@�6�Y"LS����T%�T~�{�
���1�)߂(�ߊ]�߆�'�j�����������^���!�p�M-{u��TqÄrA���f��� �r
G�e�C��/�\[��]� �˜.����G��U
                           ��(��+K������Cf7<���$�9��
                                                    l�+�X�pEޥ���.�T�5�0��G�\���]֦��r�Qs���!G�g��7�_b�H� - Error details: NoMethodError: undefined method `attributes' for nil:NilClass.
Backtrace:
["/opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/xml-simple-1.1.3/lib/xmlsimple.rb:714:in `get_attributes'", "/opt/boxen/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/xml-simple-1.1.3/lib/xmlsimple.rb:461:in `collapse'"
...

TLS 1.1 or higher

Current API doesn't appear to support TLS 1.1 or higher security protocol. Upon connection, I see the following error:
RuntimeError: UNSUPPORTED_CLIENT: TLS 1.0 has been disabled in this organization. Please use TLS 1.1 or higher when connecting to Salesforce using https.

Convert Lead into Opportunities

Hello :)

How can I convert the lead (I got all data there including the ID) into an opportunity?

Anyone can help me?

Thank you!

Query Attachments

I tried to make a query the Body field on object Attachment but the query returned an error. I am assuming that query on Blob values is not allowed, can anyone confirm this? Is there any workaround on how to retrieved the Body of an Attachment from Salesforce?

License

This gem seems to have lost traction but is quite useful.

Would you consider changing the License to Apache 2.0 or MIT so that it can be safely used for commercial projects?

Thanks!

CSV::MalformedCSVError: Illegal quoting in line 1 when update

When trying to update records, it appears to work fine from the command line but when using it inside a rake task it fails when calling get_batch_result

Relevant stack trace

/home/adan/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/csv.rb:1852:in `block (2 levels) in shift'
/home/adan/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/csv.rb:1814:in `each'
/home/adan/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/csv.rb:1814:in `block in shift'
/home/adan/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/csv.rb:1774:in `loop'
/home/adan/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/csv.rb:1774:in `shift'
/home/adan/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/csv.rb:1716:in `each'
/home/adan/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/csv.rb:1730:in `to_a'
/home/adan/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/csv.rb:1730:in `read'
/home/adan/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/csv.rb:1291:in `parse'
/home/adan/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/salesforce_bulk-1.0.3/lib/salesforce_bulk/job.rb:132:in `parse_results'
/home/adan/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/salesforce_bulk-1.0.3/lib/salesforce_bulk/job.rb:123:in `get_batch_result'

Updating picklist in Array format

I cannot pass Array as argument. Example my account has picklist that accepts array as argument. MyPicklist__c => Array. But it takes array as string

Appears to have stopped working on SF sandbox due to URL change

Sometime in the past week it no longer accesses the SF sandbox.
I think SF changed the URL so the #parse_instance regex no longer recognizes it.

This monkeypatch makes it work again:

module SalesforceBulk
 class Connection
   def parse_instance()
     m = /https:\/\/([a-z]{2,2}[0-9]{1,2})(-api|\.)/.match(@server_url)
     return '' unless m
     @instance = m[1]
   end
 end
end

The @server_url used to look something like https://cs14-api.salesforce.com/services/Soap/u/24.0/x
but now it looks like https://cs14.salesforce.com/services/Soap/u/24.0/x
The orig code wants the '-api' there.

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.