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
jhilgemanjhilgeman 

Upserting Custom Objects with Enterprise Client

I'm not sure who is maintaining the PHP Toolkit code, but I keep coming across things that don't seem to make any sense (and they seem easily fixable). For example, I just downloaded PHP Toolkit 13.1 and tried to upsert a record into a custom object.

 

Of course, there's no sample code for the upsert method for the Enterprise client - only the Partner client and that sample doesn't work at all. Inside SforceEnterpriseClient.php's upsert method, "Contact" is literally hardcoded as the object type.

 

I changed the code from:

 

  public function upsert($ext_Id, $sObjects) {
    $arg = new stdClass;
    $arg->externalIDFieldName = new SoapVar($ext_Id, XSD_STRING, 'string', 'http://www.w3.org/2001/XMLSchema');
    foreach ($sObjects as &$sObject) {
      $sObject = new SoapVar($sObject, SOAP_ENC_OBJECT, 'Contact', $this->namespace);
    }
    $arg->sObjects = $sObjects;
    return parent::_upsert($arg);
  }

 

to:

 

  public function upsert($ext_Id, $sObjects,$objectType = "Contact") {
    $arg = new stdClass;
    $arg->externalIDFieldName = new SoapVar($ext_Id, XSD_STRING, 'string', 'http://www.w3.org/2001/XMLSchema');
    foreach ($sObjects as &$sObject) {
      $sObject = new SoapVar($sObject, SOAP_ENC_OBJECT, $objectType, $this->namespace);
    }
    $arg->sObjects = $sObjects;
    return parent::_upsert($arg);
  }
 

That seems to solve the problem - I simply pass the custom object's name as the third argument, and the field list array as the second argument, and it works just fine. Why can't SalesForce just make that the default behavior? It's been hardcoded for years now... (and nobody's ever bothered to fill out the rest of the PHP Toolkit documentation)

msimondsmsimonds
I agree that the documentation and code is not updated properly in some cases and needs to be revamped. I would contact Nick Tran and see if he can help out.  Since PHP is open source, it is not well maintained here
CwestonrCwestonr

The following is a better fix to the above as it changes the function to act more like the apex api
This changes the order of the prams to move the optional prams to the end of the call.

This change is to better take advantage of Defaulting values in PHP.


/**
* Creates new objects and updates existing objects; uses a custom field to
* determine the presence of existing objects. In most cases, we recommend
* that you use upsert instead of create because upsert is idempotent.
* Available in the API version 7.0 and later.
*
* @param array $sObjects Array of sObjects
* @param string $objectType Name of Sf object to upsert to
* @param string $ext_Id Field the id matching is set to ID by default
* @return UpsertResult stdClass Object
*/
public function upsert($sObjects, $objectType = "Contact", $ext_Id ='ID' ) {
$arg = new stdClass;
$arg->externalIDFieldName = new SoapVar($ext_Id, XSD_STRING, 'string', 'http://www.w3.org/2001/XMLSchema');
foreach ($sObjects as &$sObject) {
$sObject = new SoapVar($sObject, SOAP_ENC_OBJECT, $objectType, $this->namespace);
}
$arg->sObjects = $sObjects;
return parent::_upsert($arg);
}