简体   繁体   English

Salesforce REST API:Databasedotcom gem可上传附件

[英]Salesforce REST API: Databasedotcom gem to upload attachments

I'm using Heroku's Databasedotcom gem to interact with Salesforce's REST API in my Ruby on Rails app. 我在我的Ruby on Rails应用程序中使用Heroku的Databasedotcom gem与Salesforce的REST API进行交互。 I can successfully create Accounts via the API, but I'm having trouble adding attachments. 我可以通过API成功创建帐户,但是添加附件时遇到问题。 I've found this suggestion https://github.com/heroku/databasedotcom/issues/98 and did the following: 我发现了这个建议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

When I try to save, however, I get the following error: 但是,当我尝试保存时,出现以下错误:

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>'

I've tried different files and still get the issue. 我尝试了其他文件,但仍然遇到问题。 I strongly suspect this is related to setting the "Body" attribute of the attachment, but I can't figure out what I'm doing wrong because all the resources, including Salesforces' developer docs , say to base64 encode the file. 我强烈怀疑这与设置附件的“ Body”属性有关,但是我无法弄清楚我在做什么,因为所有资源(包括Salesforces的开发人员文档 )都对base64进行了编码。 Moreover I am not exceeding their 5 Mb limit. 此外,我没有超过其5 Mb的限制。 Any suggestions??? 有什么建议么???

Ok I figured it out: when I was setting the attachment's ParentId, I was using the actual Account object, not the ID of the Account object. 好的,我知道了:在设置附件的ParentId时,我使用的是实际的Account对象,而不是Account对象的ID。 Ie: 即:

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Rails中未为此组织启用REST API(使用databasedotcom gem的Salesforce API) - The REST API is not enabled for this Organization (Salesforce API using databasedotcom gem) in Rails Salesforce Databasedotcom gem:查询不会返回正确的结果 - Salesforce databasedotcom gem: Query doesn't return right results databasedotcom gem AssignmentRules - databasedotcom gem AssignmentRules databasedotcom gem sobject问题 - databasedotcom gem sobject issue 使用databasedotcom ruby​​ gem访问Salesforce时,如何获取特定用户(或帐户)的ActivityHistory - When using databasedotcom ruby gem to access salesforce, how do I get the ActivityHistory for a particular User (or Account) 使用Databasedotcom-rails gem从RoR应用程序创建Salesforce自定义对象记录时出现NoMethodError - NoMethodError when creating a Salesforce custom object record from a RoR app with databasedotcom-rails gem Rails中的显示方法不适用于Databasedotcom gem - Show method in Rails not working with databasedotcom gem 在没有模型的情况下验证电子邮件格式-Databasedotcom gem - Validate email format without model - databasedotcom gem Ruby Gem“Databasedotcom”同样的对象问题 - Ruby Gem “Databasedotcom” same object issue 将Restforce gem与SalesForce API和Oauth 2.0一起使用 - Using Restforce gem with SalesForce API and Oauth 2.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM