我正在使用Heroku的Databasedotcom gem在我的Ruby on Rails应用程序中与Salesforce的REST API交互我可以通过api成功创建帐户,但添加附件时遇到问题。我发现这个建议https://github.com/heroku/databasedotcom/issues/98并执行了以下操作:
# 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编码。此外,我没有超过他们的5 Mb限制有什么建议吗???
最佳答案
好吧,我想起来了:当我设置附件的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
关于ruby-on-rails - Salesforce REST API:Databasedotcom gem可上传附件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23033305/