0 votes
1 view
in Salesforce by (11.9k points)
edited by

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.

1 Answer

0 votes
by (30.1k points)
edited by

In order to bulkify the API operations, you need to wrap the request in another array with some consistent symbol such as the key for each value. 

For example, in order to create two opportunities, do the following:

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])

The following XML is created behind the scenes and is sen to SDFC:

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

The response for two opportunities created at once is:

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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...