本文介绍了Ruby / Rails:如何通过rforce gem通过一个API调用在salesforce中创建多个记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 gem在我的salesforce数据库中创建记录。


$ b 在rforce文档中创建记录的示例如下:

 机会= [
:类型,'机会',
:accountId,account_id,
:金额,'10 .00',
:姓名,'Fakey McFakerson',
:closeDate,'2008-07-04',
:stageName,'Closed Won'
]

binding.create:sObject =>机会

salesforce API调用 create()允许一次创建多个对象,但我正在努力完成此任务。我已经尝试了以下调用:

  binding.create:sObject => array_of_opportunities 

其中 array_of_opportunities 是一个数组数组例如上面的例子中 opportunity



但是会引发错误:

  NoMethodError(未定义方法`to_sym'for#< Array:0x00000004ba5488>)

我很感激任何帮助。

解决方案

要批量处理API操作,请将请求包装在具有一致符号的另一个数组中(即 sObjects )作为每个值的关键字。在每个值之前应该重复相同的符号,因为它会被转换为重复的XML子元素。例如,如果您想创建两个机会,请执行以下操作:

  opportunity1 = [
:type,'Opportunity ',
:amount,'10.00',
:name,'OPP1',
:closeDate,'2008-07-04',
:stageName,'已关闭'
]

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

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

这个XML在幕后创建并发送给SFDC:

 < create xmlns =urn:partner.soap.sforce.com> 
< sObjects>
< type>商机< / type>
<金额> 10.00< /金额>
<名称> OPP1< /名称>
< closeDate> 2008-07-04< / closeDate>
< stageName>已关闭赢< / stageName>
< / sObjects>
< sObjects>
< type>商机< / type>
<金额> 10.00< /金额>
<名称> OPP2< /名称>
< closeDate> 2008-07-04< / closeDate>
< stageName>已关闭赢< / stageName>
< / sObjects>
< / create>

这里是对一次创建的两个机会的回应:

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

注意,一次最多可以创建200条记录。

另外,我注意到如果两个值是相同的确切对象c $ c> binding.create([:sObjects,opportunity1,:sObjects,opportunity1]),XML转换器会吓坏了,所以要确保它们实际上是单独的对象。框架,但在实际的生产环境中,这种情况非常罕见,可以认为是严重的,但在测试时请注意。


I'm using the rforce gem to create records in my salesforce database.

The example for creating records in the rforce documentation is:

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

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 (i.e. :sObjects) as the key for each value. The same symbol should be repeated before each value, as this gets converted into the repeated XML child elements. 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:

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

Also, I noticed that if the two values are the same exact object (i.e. 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.

这篇关于Ruby / Rails:如何通过rforce gem通过一个API调用在salesforce中创建多个记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 11:42