I want to add contacts in my salesforce account via REST api, I already found some code to access the REST API but i dont know how I will integrate it to my account since when i tried the sample codes no new users where added to my contact list.
this is the link for the code i used:
http://developer.force.com/cookbook/recipe/interact-with-the-forcecom-rest-api-from-php
the code i use:
<?php
require_once ('soapclient/SforcePartnerClient.php');
require_once ('soapclient/SforceEnterpriseClient.php');
define("USERNAME", "xxxxxx@xxxx.xxx");
define("PASSWORD", "xxxxxxxxxxxx");
define("SECURITY_TOKEN", "gHN6m4YCrrf09jlIh2r65ndN");
echo "------------------";
try {
echo "<table border=\"1\"><tr><td>";
echo "First with the enterprise client<br/><br/>\n";
$mySforceConnection = new SforceEnterpriseClient();
$mySforceConnection->createConnection("soapclient/enterprise.wsdl.xml");
if (isset($_SESSION['enterpriseSessionId'])) {
$location = $_SESSION['enterpriseLocation'];
$sessionId = $_SESSION['enterpriseSessionId'];
$mySforceConnection->setEndpoint($location);
$mySforceConnection->setSessionHeader($sessionId);
echo "Used session ID for enterprise<br/><br/>\n";
} else {
$mySforceConnection->login(USERNAME, PASSWORD.SECURITY_TOKEN);
$_SESSION['enterpriseLocation'] = $mySforceConnection->getLocation();
$_SESSION['enterpriseSessionId'] = $mySforceConnection->getSessionId();
echo "Logged in with enterprise<br/><br/>\n";
}
$query = "SELECT Id, FirstName, LastName, Phone from Contact";
$response = $mySforceConnection->query($query);
echo "Results of query '$query'<br/><br/>\n";
foreach ($response->records as $record) {
echo $record->Id.": ".$record->FirstName." "
.$record->LastName." ".$record->Phone."<br/>\n";
}
echo "<br/>Now, create some records<br/><br/>\n";
$records = array();
$records[0] = new stdclass();
$records[0]->FirstName = 'John';
$records[0]->LastName = 'Smith';
$records[0]->Phone = '(510) 555-5555';
$records[0]->BirthDate = '1957-01-25';
$records[1] = new stdclass();
$records[1]->FirstName = 'Mary';
$records[1]->LastName = 'Jones';
$records[1]->Phone = '(510) 486-9969';
$records[1]->BirthDate = '1977-01-25';
$response = $mySforceConnection->create($records, 'Contact');
$ids = array();
foreach ($response as $i => $result) {
echo $records[$i]->FirstName . " " . $records[$i]->LastName . " "
. $records[$i]->Phone . " created with id " . $result->id
. "<br/>\n";
array_push($ids, $result->id);
}
echo "<br/>Retrieve the newly created records:<br/><br/>\n";
$response = $mySforceConnection->retrieve('Id, FirstName, LastName, Phone',
'Contact', $ids);
foreach ($response as $record) {
echo $record->Id.": ".$record->FirstName." "
.$record->LastName." ".$record->Phone."<br/>\n";
}
echo "<br/>Next, update the new records<br/><br/>\n";
$records[0] = new stdclass();
$records[0]->Id = $ids[0];
$records[0]->Phone = '(415) 555-5555';
$records[1] = new stdclass();
$records[1]->Id = $ids[1];
$records[1]->Phone = '(415) 486-9969';
$response = $mySforceConnection->update($records, 'Contact');
foreach ($response as $result) {
echo $result->id . " updated<br/>\n";
}
echo "<br/>Retrieve the updated records to check the update:<br/><br/>\n";
$response = $mySforceConnection->retrieve('Id, FirstName, LastName, Phone',
'Contact', $ids);
foreach ($response as $record) {
echo $record->Id.": ".$record->FirstName." "
.$record->LastName." ".$record->Phone."<br/>\n";
}
echo "<br/>Let's remove those phone numbers<br/><br/>\n";
$records[0] = new stdclass();
$records[0]->Id = $ids[0];
$records[0]->fieldsToNull = 'Phone';
$records[1] = new stdclass();
$records[1]->Id = $ids[1];
$records[1]->fieldsToNull = 'Phone';
$response = $mySforceConnection->update($records, 'Contact');
foreach ($response as $result) {
echo $result->id . " updated<br/>\n";
}
echo "<br/>Retrieve the updated records again to check the update:";
echo "<br/><br/>\n";
$response = $mySforceConnection->retrieve('Id, FirstName, LastName, Phone','Contact', $ids);
foreach ($response as $record) {
echo $record->Id.": ".$record->FirstName." "
.$record->LastName." ".$record->Phone."<br/>\n";
}
/*
echo "<br/>Finally, delete the records:<br/><br/>\n";
$response = $mySforceConnection->delete($ids);
foreach ($response as $result) {
echo $result->id." deleted<br/>\n";
}
*/
echo "</td><td>";
echo "Now let's use the partner client<br/><br/>\n";
$mySforceConnection = new SforcePartnerClient();
$mySforceConnection->createConnection("soapclient/partner.wsdl.xml");
if (isset($_SESSION['partnerSessionId'])) {
$location = $_SESSION['partnerLocation'];
$sessionId = $_SESSION['partnerSessionId'];
$mySforceConnection->setEndpoint($location);
$mySforceConnection->setSessionHeader($sessionId);
echo "Used session ID for partner<br/><br/>\n";
} else {
$mySforceConnection->login(USERNAME, PASSWORD.SECURITY_TOKEN);
$_SESSION['partnerLocation'] = $mySforceConnection->getLocation();
$_SESSION['partnerSessionId'] = $mySforceConnection->getSessionId();
echo "Logged in with partner<br/><br/>\n";
}
$query = "SELECT Id, FirstName, LastName, Phone from Contact";
$response = $mySforceConnection->query($query);
echo '<pre>';
print_r($response);
echo '</pre>';
echo "Results of query '$query'<br/><br/>\n";
foreach ($response->records as $record) {
echo $record->Id.": ".$record->fields->FirstName." "
.$record->fields->LastName." "
.$record->fields->Phone."\n";
}
echo "<br/>Now, create some records<br/><br/>\n";
$records = array();
$records[0] = new SObject();
$records[0]->fields = array(
'FirstName' => 'John',
'LastName' => 'Smith',
'Phone' => '(510) 555-5555',
'BirthDate' => '1957-01-25'
);
$records[0]->type = 'Contact';
$records[1] = new SObject();
$records[1]->fields = array(
'FirstName' => 'Mary',
'LastName' => 'Jones',
'Phone' => '(510) 486-9969',
'BirthDate' => '1977-01-25'
);
$records[1]->type = 'Contact';
$response = $mySforceConnection->create($records);
$ids = array();
foreach ($response as $i => $result) {
echo $records[$i]->fields["FirstName"]." "
.$records[$i]->fields["LastName"]." "
.$records[$i]->fields["Phone"]
." created with id ".$result->id."<br/>\n";
array_push($ids, $result->id);
}
echo "<br/>Retrieve the newly created records:<br/><br/>\n";
$response = $mySforceConnection->retrieve('Id, FirstName, LastName, Phone',
'Contact', $ids);
foreach ($response as $record) {
echo $record->Id.": ".$record->fields->FirstName." "
.$record->fields->LastName." "
.$record->fields->Phone."<br/>\n";
}
echo "<br/>Next, update the new records<br/><br/>\n";
$records[0] = new SObject();
$records[0]->Id = $ids[0];
$records[0]->fields = array(
'Phone' => '(415) 555-5555',
);
$records[0]->type = 'Contact';
$records[1] = new SObject();
$records[1]->Id = $ids[0];
$records[1]->fields = array(
'Phone' => '(415) 486-9969',
);
$records[1]->type = 'Contact';
$response = $mySforceConnection->update($records);
foreach ($response as $result) {
echo $result->id." updated<br/>\n";
}
echo "<br/>Retrieve the updated records to check the update:<br/><br/>\n";
$response = $mySforceConnection->retrieve('Id, FirstName, LastName, Phone',
'Contact', $ids);
foreach ($response as $record) {
echo $record->Id.": ".$record->fields->FirstName." "
.$record->fields->LastName." ".$record->fields->Phone."<br/>\n";
}
echo "<br/>Let's remove those phone numbers<br/><br/>\n";
$records[0] = new SObject();
$records[0]->Id = $ids[0];
$records[0]->fieldsToNull = 'Phone';
$records[0]->type = 'Contact';
$records[1] = new SObject();
$records[1]->Id = $ids[1];
$records[1]->fieldsToNull = 'Phone';
$records[1]->type = 'Contact';
$response = $mySforceConnection->update($records);
foreach ($response as $result) {
echo $result->id." updated<br/>\n";
}
echo "<br/>Retrieve the updated records again to check the update:";
echo "<br/><br/>\n";
$response = $mySforceConnection->retrieve('Id, FirstName, LastName, Phone',
'Contact', $ids);
foreach ($response as $record) {
echo $record->Id.": ".$record->fields->FirstName." "
.$record->fields->LastName." ".$record->fields->Phone."<br/>\n";
}
/* echo "<br/>Finally, delete the records:<br/><br/>\n";
$response = $mySforceConnection->delete($ids);
foreach ($response as $result) {
echo $result->id." deleted<br/>\n";
}
*/
echo "</td></tr></table>";
} catch (Exception $e) {
echo "Exception ".$e->faultstring."<br/><br/>\n";
echo "Last Request:<br/><br/>\n";
echo $mySforceConnection->getLastRequestHeaders();
echo "<br/><br/>\n";
echo $mySforceConnection->getLastRequest();
echo "<br/><br/>\n";
echo "Last Response:<br/><br/>\n";
echo $mySforceConnection->getLastResponseHeaders();
echo "<br/><br/>\n";
echo $mySforceConnection->getLastResponse();
}
?>
I commented what i believe to be the deleting function part. still i dont see any of those contacts in my list or what do i have to do to see those contacts or where are they being added?
Any help will appreciated, I am just new to salesforce and want to integrate it to our system
thank you