function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jordan LevJordan Lev 

Inserting/Updating multiple records

Does anyone know how to call update() for more than one object at a time (using WebService_sforceService_Soal.php)? Here is the code I have for updating one record:
     $client = new SalesforceClient($SESSION);
     $client->_endpoint = $_COOKIE['sf_endpoint'];
    
     $myFields = array('type'=>'lead', 'Id'=>'00S20000007k888AAA', 'Company'=>'testCo');
     $result = $client->update($myFields);

This code works perfectly, but I cannot figure out how to pass more than one "sObject" (php array) to it. My first guess was to make $myFields an array of arrays, but that didn't work. Then I tried just adding more and more key/value pairs to the $myFields array ('type'=>'lead', 'Id'=>'00S20000007k888AAA', 'Company'=>'testCo1', 'type'=>'lead', 'Id'=>'00S20000007k9999BB', 'Company'=>'testCo2', etc. etc.) but that didn't work either.

Thanks for any help you can provide (I really don't want to make multiple calls to the update function).

-Jordan Lev
Jordan LevJordan Lev
Well this is unbelievable to me but I actually figured out how to get this working! So for anyone else who is wondering, here's what you have to do to perform multiple updates/creates/deletes with one call:

1) You need to alter the WebService_SforceService_Soap.php file's functions (in this example I use the create() function but the same process applies to update() and delete() as well). Change it from:

  function &update($sObjects) {
    $update =& new SOAP_Value('{urn:partner.soap.sforce.com}update', false, $v=array("sObjects"=>$sObjects));
    return $this->call("update",
        $v = array('update'=>$update),
        array('namespace'=>'urn:partner.soap.sforce.com',
          'soapaction'=>'',
          'style'=>'document',
          'use'=>'literal' ));
  }

to:

  function &update($sObjects) {
    $updates =& new SOAP_Value('{urn:partner.soap.sforce.com}update', false, $v=$sObjects);
    return $this->call("update",
        $v = $updates,
        array('namespace'=>'urn:partner.soap.sforce.com',
          'soapaction'=>'',
          'style'=>'document',
          'use'=>'literal' ));
  }

~Note how we simply take out the 'array()' function in both the 'new SOAP_Value' and the '$this->call' call. This function was expecting one array (one sObject), and subsequentally turning that one sObject into an array of the one sObject. But we want to be able to pass an array of arrays (array of sObjects) to this function, so we don't want to wind up with an array of arrays of arrays (got that?).
~Note that I also changed the name of the '$update' variable to '$updates', because I'm **bleep**.

2) When you call the update() function in your code, you need to pass it arrays of sObjects. Similarly, when retrieving error messages, you need to remember that the $result will be an array of sObjects. So my code now looks like this:

  $client = new SalesforceClient($SESSION);
  $client->_endpoint = $_COOKIE['sf_endpoint'];

  $myLeads = array();
  $sObject = array('type'=>'lead', 'Id'=>'00X20000007q123456', 'Company'=>'test Co.');
  $myLeads[] = $sObject;
  $sObject = array('type'=>'lead', 'Id'=>'00X20000007qABCDEF', 'Company'=>'test Inc.');
  $myLeads[] = $sObject;
  //etc. etc...

  $result = $client->update($myLeads);

  foreach($result as $val) {
    if ($val->success == "true") {
      echo "SUCCESS! ID: " . $val->id . "<br />\n";
    } else {
      echo "FAILURE: " . $val->errors->message . "<br />\n";
    }
  }

~The sample code that comes with the wrapper shows an example like this:
  $FIELDS = array('type'=>'lead','FirstName'=>'newtest','LastName'=>'test','Company'=>'testthis');
  $client->create($FIELDS);
If you have already changed your create() function in WebService_SforceService_Soap.php (see step 1 above), then you would need to do something like this to get the sample code to work:
  $sObjects = array(array('type'=>'lead','FirstName'=>'newtest','LastName'=>'test','Company'=>'testthis'));
  $client->create($sObjects);


**Question to adamg: Is this going to adversely affect functionality? I kept that $v=$sObjects thing in there, although I can't see what the $v is for (perhaps some other module uses it for displaying errors?). Let me know if this is all "kosher".

Hope this helps (and thanks so much to the people who wrote these php wrappers in the first place -- you rock!)

-Jordan Lev
HarryBHarryB

Hi Jordan,

just stumbled over the same problem and then found your fix here. Tried it and it works like a charm!

Thank you! :-)

Cheers,

Harald

dminkdmink
Thank you!!!

(Just wish I could've found this sooner!)
spikeJonzespikeJonze
Awesome stuff, but what about the retrieve call? It takes 3 arguments rather than 1, which apparently needs to be packaged into a single parameter for the &new SOAP_Value() call. I've tried a few combinations but it still returns a SAX Parser error.

any help greatly appreciated

aj
spikeJonzespikeJonze
Found my own solution: in the retrieve call the SOAP_Value attribute argument takes a 1-dimensional array containing the fieldlist, the object type, and then any id's as the remaining array values.
cwoodcwood
I must be dim--I can't get retrieve to work with multiple ID's. Are you saying I need to change the code for 'function &retrieve'? I've tried, but I still get SAX errors. Could you step me through your fix?

Thanks!
-Charlie
cwoodcwood
I found the solution to this bug.

http://forums.sforce.com/sforce/board/message?board.id=PerlDevelopment&message.id=604

Thanks!