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
venemavenema 

Upsert Problem

I'm having a problem with the upsert() function in the API.
 
I'm trying to update a single Contact record, and the result I get back from SalesForce is as follows:
 
stdClass Object
(
    [created] =>
    [id] => 00300000004NL8rAAG
    [success] => 1
)
 
This indicates to me that the update was performed successfully.  In fact, when I check in the web UI, the last modified time has been updated accordingly to show that something was done.
 
The problem is that nothing changes in the record.  I'm supposed to be updating the address, phone number, and title of this person, and yet nothing changes that I can see.
 
Any ideas?
tocktock
Hello,
  Is it possible to see the script calling the upsert?
cheers
venemavenema

Certainly.  It's a function in my class.SalesForceAPI.php:

Code:

function importContacts( $data, $mapping )
{
 // format mapping for SilverPop -> SalesForce
 $mapping = array_flip( $mapping );
 
 // separate header row
 $header = $data[0];
 $data = array_slice( $data, 1 );
 
 // initialize contacts array
 $contacts = array();
 $externalID = "Id";
 
 // build contact classes
 foreach( $data as $record )
 {
  $contact = new stdclass();
  $contact->type = 'Contact';
  
  // process contact mapped fields
  $fields = array();
  for( $i = 0; $i < count( $header ); $i++ )
  {
   if( !empty( $record[$i] ) && array_key_exists( $header[$i], $mapping ) )
   {
    $field = $mapping[$header[$i]];
    $fields[$field] = $this->prepare( $record[$i] );
    if( $field == $externalID )
     $contact->$field = $this->prepare( $record[$i] );
   }
  }
  $contact->fields = $fields;
  $contacts[] = $contact;
 }
 
 // upsert data (update/insert)
 $upsertResult = $this->api->upsert( $externalID, $contacts );

 return $upsertResult;
}


 
The function takes two inputs:

1) $data is a two-dimensional array version of a CSV file.  The first row contains headers, the second, third, fourth, etc. rows contain values.

2) $mapping is a one-dimensional array that maps SalesForce field names to the header names in the data file.  The first operation in the function flips the keys/values so it's the header names mapping to the SalesForce field names.

Code:

// SalesForce field name => SilverPop field name
$mapping = array(
  "Id"
   => "SalesForce ID",
  "Email"
   => "EMAIL",
  "LastName"
   => "Last Name",
  "FirstName"
   => "First Name"
 );


After running the function on a data file with a single record, I get an array of stdclass objects that looks like this:

Code:

Array
(
    [0] => stdClass Object
        (
            [type] => Contact
            [Id] => 00300000004NL8rAAG
            [fields] => Array
                (
                    [FirstName] => Anton
                    [LastName] => Venema
                    [Phone] => (604) 628-5224
                    [Id] => 00300000004NL8rAAG
                )

        )

)

 

venemavenema

Sorry, the mapping for this example looks like this:

Code:

$mapping = array(
  "Id"
   => "SalesForce ID",
  "LastName"
   => "Last Name",
  "FirstName"
   => "First Name",
  "Phone"
   => "Phone"
 );


 

venemavenema

I used tcpmon to get the actual TCP request being sent to SalesForce.  Looks like this:

Code:

<—xml version="1.0" encoding="UTF-8"–>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:enterprise.soap.sforce.com" xmlns:ns2="urn:sobject.enterprise.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <SOAP-ENV:Header>
  <ns1:SessionHeader>
   <ns1:sessionId>68B0vV7RRjbIeGXiMsqAQqKA1Z3FG.XESCGKXnZjflSanJCE4B.eVfUSN7BeoExQwX4HHFsgaeCgqvJINsBwpqlV42zA6fc8k8FDDBHSEG3ZJKzYAJI=</ns1:sessionId>
  </ns1:SessionHeader>
 </SOAP-ENV:Header>
 <SOAP-ENV:Body>
  <ns1:upsert>
   <ns1:externalIDFieldName>Id</ns1:externalIDFieldName>
   <ns1:sObjects xsi:type="ns2:Contact">
    <ns2:Id>00300000004NL8rAAG</ns2:Id>
   </ns1:sObjects>
  </ns1:upsert>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>


 
It looks to me like the object being submitted is empty...