In the official documentation there are examples of how to do it in Java and C# but there are no examples of how to do it in PHP using Salesforce PHP Toolkit:

See paragraph "create() and Foreign Keys" here:


Example of create call using PHP toolkit:


Any help would be much appreciated!

Thank you!

  • for the External_ID_Field__c its required same name in both objects? or we can use any field which is carrying same value in both object Commented Jul 18, 2013 at 8:26
  • Yes, you have to put the same value for External_ID_Field__c on both objects. You can only use fields which are marked as external ID fields.
    – Roger
    Commented Aug 20, 2013 at 12:01

1 Answer 1


Long ago I saw this solution on the developerforce message boards, you have to modify the PHP toolkit so that the _convertToAny() function found in SforceBaseClient.php looks like this:

protected function _convertToAny($fields) {
    $anyString = '';
    foreach ($fields as $key => $value) {
        if($value instanceOf stdclass || $value instanceOf SObject) {
            $anyString = $anyString . '<' . $key . ' xmlns="">';
            $anyString = $anyString . '<type xmlns="urn:sobject.partner.soap.sforce.com">' . $value->type . '</type>';
            $anyString = $anyString . $this->_convertToAny($value->fields);
            $anyString = $anyString . '</'. $key . '>';
            $anyString = $anyString . '<' . $key . '>' . $value . '</' . $key . '>';
    return $anyString;

Now you can do cascade inserts like so:

// create SOAP connect
$mySforceConnection = new SforcePartnerClient();
$mySoapClient = $mySforceConnection->createConnection($pathToPartnerWsdl);
$mylogin = $mySforceConnection->login($username, $password);

// create a Contact
$contactObj = new SObject();
$contactObj->type = 'Contact';
$contactObj->fields = array(
    'FirstName' => 'Some',
    'LastName' => 'Guy',
    'Email' => '[email protected]'

// create an Account
$accountObj = new SObject();
$accountObj->type = 'Account';  
$accountObj->fields = array(
    'Name' => 'This is an Account',
    'External_ID_Field__c' => 'XXYYZZ'

// create a reference (this can only have the External ID field, trust me)
$accountRef = new SObject();
$accountRef->type = 'Account';  
$accountRef->fields = array(
    'External_ID_Field__c' => 'XXYYZZ'

$contactObj->fields['Account'] = $accountRef;

// insert the sObjects (this only works if the parent comes first)
$createResponse = $mySforceConnection->create(array($accountObj, $contactObj));

  • Thanks for your reply. Tomorrow I'll try this solution. If it works I'll accept and +1 your answer.
    – Roger
    Commented Jul 15, 2013 at 17:19
  • It does not work. Error message shows up: SoapFault: Element {}item invalid at this location in SoapClient->__call(). The error comes from SforceBaseClient.php, function _create, return statement. I'm using the version 20.0 of PHP Toolkit. Another question is, what I'm supposed to put instead of XXYYZZ string? Thank you!
    – Roger
    Commented Jul 16, 2013 at 9:04
  • I've tested this sample myself, it definitely works. Sounds like you are providing the enterprise WSDL to the partner client. If you want to use the enterprise WSDL you need to change the way the SObjects are declared in this code (see the force.com PHP toolkit samples for help). 'XXYYZZ' is the external ID value that the records are linking on, you just put whatever you want your external ID to be here. Commented Jul 16, 2013 at 9:26
  • Ok, the error disappeared using the partner WSDL. Now I have the following error: "SoapFault: INVALID_FIELD: No such column 'External_ID_Field__c' on entity 'Account'" Should I create this column and refresh WSDL to solve the problem? Thank you!
    – Roger
    Commented Jul 16, 2013 at 16:20
  • Well, I've just done it and now another error appears: SoapFault: Unexpected element {urn:sobject.partner.soap.sforce.com}type during simple type deserialization in SoapClient->__call() Again, the error comes from SforceBaseClient.php, function _create, return statement. What I'im doing wrong? Are you also using the 20.0 version of toolkit? Thanks!
    – Roger
    Commented Jul 16, 2013 at 16:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .