简体   繁体   English

Ruby / Rails:如何通过rforce gem通过一次API调用在salesforce中创建多个记录?

[英]Ruby / Rails: how can I create multiple records in salesforce with one API call via the rforce gem?

I'm using the rforce gem to create records in my salesforce database. 我正在使用rforce gem在salesforce数据库中创建记录。

The example for creating records in the rforce documentation is: 在rforce文档中创建记录的示例是:

  opportunity = [
                 :type,      'Opportunity',
                 :accountId, account_id,
                 :amount,    '10.00',
                 :name,      'Fakey McFakerson',
                 :closeDate, '2008-07-04',
                 :stageName, 'Closed Won'
                ]

  binding.create :sObject => opportunity

The salesforce API call create() allows for the creation of multiple object at once, but I'm struggling to accomplish this. salesforce API调用create()允许一次创建多个对象,但我正在努力实现这一目标。 I've tried the following call: 我试过以下电话:

binding.create :sObject => array_of_opportunities

Where array_of_opportunities is an array of arrays like opportunity in the example above. 其中array_of_opportunities是一个数组数组,例如上面示例中的opportunity

but that throws an error: 但是会抛出一个错误:

NoMethodError (undefined method `to_sym' for #<Array:0x00000004ba5488>)

I'd appreciate any help. 我很感激任何帮助。

To bulkify the API operations, wrap the request in another array with some consistent symbol (ie :sObjects ) as the key for each value. 要批量化API操作,请将请求包装在另一个数组中,并使用一些一致的符号(即:sObjects )作为每个值的键。 The same symbol should be repeated before each value, as this gets converted into the repeated XML child elements. 应在每个值之前重复相同的符号,因为这会转换为重复的XML子元素。 For example, if you want to create two opportunities, do this: 例如,如果要创建两个商机,请执行以下操作:

opportunity1 = [
    :type,      'Opportunity',
    :amount,    '10.00',
    :name,      'OPP1',
    :closeDate, '2008-07-04',
    :stageName, 'Closed Won'
]

opportunity2 = [
    :type,      'Opportunity',
    :amount,    '10.00',
    :name,      'OPP2',
    :closeDate, '2008-07-04',
    :stageName, 'Closed Won'
]

puts binding.create([:sObjects, opportunity1, :sObjects, opportunity2])

This XML is created behind the scenes and sent to SFDC: 此XML在幕后创建并发送到SFDC:

<create xmlns="urn:partner.soap.sforce.com">
  <sObjects>
    <type>Opportunity</type>
    <amount>10.00</amount>
    <name>OPP1</name>
    <closeDate>2008-07-04</closeDate>
    <stageName>Closed Won</stageName>
  </sObjects>
  <sObjects>
    <type>Opportunity</type>
    <amount>10.00</amount>
    <name>OPP2</name>
    <closeDate>2008-07-04</closeDate>
    <stageName>Closed Won</stageName>
  </sObjects>
</create>

and here is the response for the two opportunities being created at once: 以下是对同时创建的两个机会的回应:

{:createResponse=>{:result=>[{:id=>"0066000000KNMrOAAX", :success=>"true"}, {:id=>"0066000000KNMrPAAX", :success=>"true"}]}}

Note, you can create up to 200 records at a time. 请注意,您一次最多可以创建200条记录。

Also, I noticed that if the two values are the same exact object (ie doing something like binding.create([:sObjects, opportunity1, :sObjects, opportunity1]) , the XML converter freaks out, so make sure they are actually separate objects. This is probably a bug in the framework, but it is such a rare case in actual production situations to be considered serious, but watch out for it while you are testing. 另外,我注意到如果这两个值是完全相同的对象(即执行类似binding.create([:sObjects, opportunity1, :sObjects, opportunity1])事情,那么XML转换器会吓坏,所以请确保它们实际上是单独的对象这可能是框架中的一个错误,但在实际生产情况下这种情况很少被认为是严重的,但在测试时要注意它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM