developerforce / Force.com-Toolkit-for-PHP

Other
262 stars 215 forks source link

Add support for multi-record mergeRequests #14

Open messageagency opened 12 years ago

messageagency commented 12 years ago

According to the documentation for merge http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_merge.htm up to 200 merge requests can be issued in a single SOAP call.

However, both SforcePartnerClient::merge() and SforceEnterpriseClient::merge() only accept a single merge request object, rather than an array of merge requests, and the WSDL definition of merge() doesn't appear to support multiple mergeRequests.

Also, the sample code in merge documentation does not show any examples issuing multiple mergeRequests per SOAP call. Am I reading the documentation wrong?

My attempt at a patch resulted in the following fatal error: Uncaught SoapFault exception: [Client] SOAP-ERROR: Encoding: object hasn't 'masterRecord' property

messageagency commented 12 years ago

OK, I have a working patch for Enterprise Client, and an untested patch based on the same principal for Partner Client. Apparently I can't attach a file in a github issue, so here are the patches:

Index: soapclient/SforcePartnerClient.php
===================================================================
--- soapclient/SforcePartnerClient.php  (revision 2906)
+++ soapclient/SforcePartnerClient.php  (working copy)
@@ -63,19 +63,27 @@
   /**
    * Merge records
    *
-   * @param stdclass $mergeRequest
+   * @param mixed $mergeRequests single mergerequest object or an array of
+   *   mergerequests
    * @param String $type
    * @return mixed
    */
-  public function merge($mergeRequest) {
-    if (isset($mergeRequest->masterRecord)) {
+  public function merge($mergeRequests) {
+    if (is_object($mergeRequests)) {
+      $mergeRequests = array($mergeRequests);
+    }
+    $arg = new stdClass;
+    $arg->request = array();
+    foreach ($mergeRequests as &$mergeRequest) {
+      if (!isset($mergeRequest->masterRecord)) {
+        continue;
+      }
       if (isset($mergeRequest->masterRecord->fields)) {
-        $mergeRequest->masterRecord->any = $this->_convertToAny($mergeRequest->masterRecord->fields);
+          $mergeRequest->masterRecord->any = $this->_convertToAny($mergeRequest->masterRecord->fields);
       }
-      //return parent::merge($mergeRequest, $type);
-      $arg->request = $mergeRequest;
-      return $this->_merge($arg);
+      $arg->request[] = $mergeRequest;
     }
+    return $this->_merge($arg);
   }

   /**
Index: soapclient/SforceEnterpriseClient.php
===================================================================
--- soapclient/SforceEnterpriseClient.php   (revision 2694)
+++ soapclient/SforceEnterpriseClient.php   (working copy)
@@ -152,14 +152,21 @@
   /**
    * Merge records
    *
-   * @param stdclass $mergeRequest
+   * @param mixed $mergeRequests single mergerequest object or an array of 
+   *   mergerequests
    * @param String $type
    * @return unknown
    */
-  public function merge($mergeRequest, $type) {
-    $mergeRequest->masterRecord = new SoapVar($mergeRequest->masterRecord, SOAP_ENC_OBJECT, $type, $this->namespace);
+  public function merge($mergeRequests, $type) {
     $arg = new stdClass;
-    $arg->request = new SoapVar($mergeRequest, SOAP_ENC_OBJECT, 'MergeRequest', $this->namespace);
+    if (is_object($mergeRequests)) {
+      $mergeRequests = array($mergeRequests);
+    }
+    foreach ($mergeRequests as &$mergeRequest) {
+      $mergeRequest->masterRecord = new SoapVar($mergeRequest->masterRecord, SOAP_ENC_OBJECT, $type, $this->namespace);
+      $mergeRequest = new SoapVar($mergeRequest, SOAP_ENC_OBJECT, 'MergeRequest', $this->namespace);
+    }
+    $arg->request = $mergeRequests;
     return parent::_merge($arg);
   }
 }