2

我正在使用Heroku的Databasedotcom gem在我的Ruby on Rails應用程序中與Salesforce的REST API進行交互。我可以通過API成功創建帳戶,但是我無法添加附件。我發現這個建議https://github.com/heroku/databasedotcom/issues/98,也做了以下內容:Salesforce REST API:Databasedotcom gem上傳附件

# Upload to Salesforce attachment 
salesforce_attachment = Salesforce.client.materialize("Attachment").new 
salesforce_attachment.Body = Base64::encode64(File.read(temp_file_path)) # critical line 
salesforce_attachment.OwnerId = Salesforce::Sobjects::OWNER_IDS[:OWNER_ID] 
salesforce_attachment.ParentId = Salesforce.find_or_create_account_for_user(user) 
salesforce_attachment.Name = file_name 
salesforce_attachment.IsPrivate = false 
salesforce_attachment.save # this isn't saving for some reason 

當我試圖挽救,但我得到以下錯誤:

Databasedotcom::SalesForceError: Cannot deserialize instance of reference from START_OBJECT value { at [line:1, column:2] 
from /Users/oregontrail256/.rvm/gems/ruby-1.9.3-p545/gems/databasedotcom-1.3.2/lib/databasedotcom/client.rb:376:in `ensure_expected_response' 
from /Users/oregontrail256/.rvm/gems/ruby-1.9.3-p545/gems/databasedotcom-1.3.2/lib/databasedotcom/client.rb:334:in `with_encoded_path_and_checked_response' 
from /Users/oregontrail256/.rvm/gems/ruby-1.9.3-p545/gems/databasedotcom-1.3.2/lib/databasedotcom/client.rb:307:in `http_post' 
from /Users/oregontrail256/.rvm/gems/ruby-1.9.3-p545/gems/databasedotcom-1.3.2/lib/databasedotcom/client.rb:234:in `create' 
from /Users/oregontrail256/.rvm/gems/ruby-1.9.3-p545/gems/databasedotcom-1.3.2/lib/databasedotcom/sobject/sobject.rb:115:in `save' 
from (irb):100 
from /Users/oregontrail256/.rvm/gems/ruby-1.9.3-p545/gems/railties-3.2.17/lib/rails/commands/console.rb:47:in `start' 
from /Users/oregontrail256/.rvm/gems/ruby-1.9.3-p545/gems/railties-3.2.17/lib/rails/commands/console.rb:8:in `start' 
from /Users/oregontrail256/.rvm/gems/ruby-1.9.3-p545/gems/railties-3.2.17/lib/rails/commands.rb:41:in `<top (required)>' 
from script/rails:6:in `require' 
from script/rails:6:in `<main>' 

我已經嘗試了不同的文件,並仍然得到問題。我強烈懷疑這與設置附件的「Body」屬性有關,但我無法弄清楚我做錯了什麼,因爲包括Salesforces' developer docs在內的所有資源都會對base64進行編碼。而且我不超過他們的5Mb限制。有什麼建議麼???

回答

0

好吧我想通了:當我設置附件的ParentId時,我使用的是實際的Account對象,而不是Account對象的ID。即:

# wrong 
salesforce_attachment.ParentId = Salesforce.find_or_create_account_for_user(user) 

# right 
salesforce_attachment.ParentId = Salesforce.find_or_create_account_for_user(user).Id