关于 ruby?? on rails:Salesforce REST API:Databasedotcom gem 上传附件

Salesforce REST API: Databasedotcom gem to upload attachments

我正在使用 Heroku 的 Databasedotcom gem 在我的 Ruby on Rails 应用程序中与 Salesforce 的 REST API 进行交互。我可以通过 API 成功创建帐户,但我无法添加附件。我发现了这个建议 https://github.com/heroku/databasedotcom/issues/98 并做了以下事情:

1
2
3
4
5
6
7
8
# 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

但是,当我尝试保存时,我收到以下错误:

1
2
3
4
5
6
7
8
9
10
11
12
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>'

我尝试了不同的文件,但仍然遇到问题。我强烈怀疑这与设置附件的"正文"属性有关,但我不知道我做错了什么,因为所有资源,包括 Salesforce 的开发人员文档,都说对文件进行 base64 编码。此外,我没有超过他们的 5 Mb 限制。有什么建议吗???


好的,我想通了:当我设置附件的 ParentId 时,我使用的是实际的 Account 对象,而不是 Account 对象的 ID。即:

1
2
3
4
5
# 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