+ Start a Discussion

upsert problem on a Custom Object via PHP (php toolkit)

I am trying to do an upsert on a custom object I created and can't seem to get it to work. I was going to the upsert sample provided in the PHP Toolkit area. I am able to create a new record just fine, but upsert gives the following error (here is just the start of the error)
SoapFault Object ( [message:protected] => INVALID_FIELD: Field name provided, NewUserID__c does not match an External ID for Contact ..
Some background. New custom object is called "New User" (New_User__C to the api) and has an external ID named NewUserID which is a text field which must be unique. All I am trying to do is change the value of one of the fields and do an update (in this example I am trying to alter the Last Name field of my record).

Here is the PHP code I am running:

define("SOAP_CLIENT_BASEDIR", "./soapclient");
require_once (SOAP_CLIENT_BASEDIR.'/SforceEnterpriseClient.php');
require_once (SOAP_CLIENT_BASEDIR.'/SforceHeaderOptions.php');

try {
  $mySforceConnection = new SforceEnterpriseClient();
  $mySoapClient = $mySforceConnection->createConnection(SOAP_CLIENT_BASEDIR.'/enterprise.wsdl.xml');
  $mylogin = $mySforceConnection->login("my username", "my password");

  $sObject = new stdclass();
  $sObject->Email_Address__c = 'fake@svc.com';
  $sObject->First_Name__c = 'UserFirst';
  $sObject->Last_Name__c = 'UserLast';
  $sObject->NewUserID__c = 'aaa';

  $createResponse = $mySforceConnection->create(array($sObject), 'New_User__c');

  $sObject->Last_Name__c = 'UserLast_Change';

  $upsertResponse = $mySforceConnection->upsert("NewUserID__c", array ($sObject));

} catch (Exception $ex) {
  echo $mySforceConnection->getLastRequestHeaders();
  echo $mySforceConnection->getLastRequest();

So, the create statement works fine, it's just the upsert that doesn't work. What am I missing about how upsert is supposed to work? I'm pretty sure I am not grasping the way External IDs are supposed to be setup. Also, looking at the error message, it seems like it is trying to work on the Contact object?

Thanks for any help!

Message Edited by Ako on 12-05-2007 08:40 PM
Best Answer chosen by Admin (Salesforce Developers) 
Park Walker (TAGL)Park Walker (TAGL)
First, if you are going to use upsert there's no reason to use 'create'. The idea behind upsert is that you can pass
in a record with an exernal Id and it is created if it does not exist and updated if it does, based on the external Id value.
You can accomplish the same thing with a create call and an update call (and no external Id) if you already know
the status of the record.

Your 'create' call is incorrect (interesting that there's no error): it just takes an array of typed objects:
  $sObject->type = 'New_User__c';
  $createResponse = $mySforceConnection->create(array($sObject));

As for the upsert call, it looks like NewUser_ID__c field has not been defined as an external Id field. You need to
check the External Id field checkbox in the field definition to make that happen. The API apparently uses the value
of the external Id field to lookup the type of the objects being passed.