Skip to Content

TheBlogReaders.com

Salesforce.com, PHP, MySQL, Javascript, Ajax, Htacces

UPSERT the Salesforce Contact Record using PHP

Be First!
by January 25, 2013 PHP, SALESFORCE

UPSERT the Salesforce Contact Record using PHP

<?php
ini_set(“soap.wsdl_cache_enabled”, “0”);

// PHP Tool Kit class scripts to login to Salesforce Org
require_once (‘../includes/soapclient/SforcePartnerClient.php’);
require_once (‘../includes/soapclient/SforceHeaderOptions.php’);

// Salesforce Login information
$wsdl = ‘partner.wsdl.xml’;    // PARTNER WSDL FILE PATH
$userName = “salesforce username”;
$password = “salesforce password”; // PASSWORD SHOULD BE COMBINATION OF “PASSWORD + SECURITY TOKEN”, if it’s a Developer ORG.

// Process of logging on and getting a salesforce.com session
$client = new SforcePartnerClient();
$client->createConnection($wsdl);
$loginResult = $client->login($userName, $password);

//Then perform to Update Your Data to Salesforce Contact Object

$createFields = array (
‘FirstName’ => ‘George’,
‘LastName’ => ‘Smith’,
‘Phone’ => ‘510-555-5555’,
‘BirthDate’ => ‘1927-01-25’,
‘Email’ => ‘test@example.com’
);

$sObject = new stdclass();
$sObject->fields = $createFields;
$sObject->type = ‘Contact’;

echo “Creating New Contact\n”;
$createResponse = $loginResult->create(array($sObject));
print_r($createResponse);

$sObject->fields[‘FirstName’] = ‘Bill’;
$sObject->fields[‘LastName’] = ‘Clinton’;

echo “Upserting Contact (existing)\n”;
$upsertResponse = $loginResult->upsert(“Email”, array ($sObject));
print_r($upsertResponse);

$sObject->fields[‘FirstName’] = ‘John’;
$sObject->fields[‘LastName’] = ‘Smith’;
$sObject->fields[‘Email’] = ‘testNew@example.com’;

echo “Upserting Contact (new)\n”;
$upsertResponse = $loginResult->upsert(“Email”, array ($sObject));
print_r($upsertResponse);

} catch (Exception $ex) {
print_r($ex);
echo $loginResult->getLastRequest();
print_r($ex);
}
?>

(1243)

Previous
Next

Leave a Reply