When working with Salesforce’s PHP Toolkit, like any other database, you have to connect the dots in the background when creating relating records. Take for example a list of people that belong to a certain company or employees that may be part of a specific department. In each scenario, you have separate lists of data and there has to be some kind of glue that relates them.
In the case of Salesforce and creating new contacts and a company (account) for the contact, you’ll have to do the same and associate the contact to the account once the records are created. In the great world of databases, this is similar to populating the foreign key column to a record in a table.
In previous examples such as this post, I’ve used an include file containing the Salesforce Connection parameters and some procedural PHP for passing or retrieving data. In this example, I’ll use a Salesforce object that includes the connection, as well as the functions for the various operations we’re performing.
Creating the Salesforce Class
class Salesforce { //Contact Information var $first_name = ''; var $last_name = ''; var $email_address = ''; var $company_name = ''; //Salesforce Contact and Account var $contact_id = ''; var $account_id = ''; //Connection to Salesforce var $connection = ''; //Constructor function __construct() { //Initiation a connection to Salesforce $this->$connection = $this->connect_salesforce(); } //Connecting to Salesforce function connect_salesforce() { try { $username = "YOUR_USERNAME"; $password = "YOUR_PASSWORD"; $security_token = "YOUR_TOKEN"; $wsdl = "/PATH_TO_PHP_TOOLKIT/soapclient/partner.wsdl.xml"; require_once ('PATH_TO_PHP_TOOLKIT/soapclient/SforcePartnerClient.php'); $this->connection = new SforcePartnerClient(); $this->connection->createConnection($wsdl); $this->connection->login($username, $password.$security_token); } catch (Exception $e) { throw new Exception( 'Could not connect to Salesforce', 0, $e); } } }
Adding a Contact Function to the class
Now we’ve got the class created. We’re going to add a function to the class that allows us to initiate the connection and create a new contact. Of course, there are many fields you can add to your contact, for this example, we’re only working with the name and email address.
function create_contact() { try { $record = array( 'FirstName' => $this->first_name, 'LastName' => $this->last_name, 'Email' => $this->email_address ); //Creating the Contact $contact = new stdClass; $contact->type = 'Contact'; $contact->fields = $record; $response = $this->connection->create(array($contact), 'Contact'); //Storing the Contact ID $this->contact_id = $response[0]->id; } catch (Exception $e) { throw new Exception( 'Could not create a contact for '.$this->first_name.' '.$this->last_name, 0, $e); } }
Adding an Account Function to the class
The previous function creates our contact record in the Contact object. We’ll now write another function that allows us to create a company in the Account object.
function create_account() { try { $record = array( 'Name' => $this->company_name ); //Creating the Company $company = new stdClass; $company->type = 'Account'; $company->fields = $record; $response = $this->connection->create(array($company), 'Account'); //Storing the Account ID $this->account_id = $response[0]->id); } catch (Exception $e) { throw new Exception( 'Could not create an account for '.$this->company_name, 0, $e); } }
Adding a function that associates the Contact and the Account
Now you’ve got a contact and a company. As far as Salesforce is concerned, these are two records that have nothing to do with each other. The final step is the ensure that Salesforce knows this new contact belongs to this new company. This will be possible because we now have the contact and account ID that was generated by Salesforce when we created them, and the association will take place on the contact’s record.
You’ll notice here that we’re not tracking the response code and we’re using the Update command instead of Create.
//Associate Account to the Contact record function associate_contact_account() { try { //Update the Contact with the Account $record = array( 'Id' => $this->contact_id, 'AccountId' => $this->account_id ); //Submitting the Account Id to the Contact Record $contact = new stdClass; $contact->type = 'Contact'; $contact->fields = $record; $this->connection->update(array($contact), 'Contact'); } catch (Exception $e) { throw new Exception( 'Could not associate the contact to the account for '.$this->first_name.' '.$this->last_name, 0, $e); } }
Working around duplicate Company records
We’ve created our contact(s) and company. However, if you plan on taking this a step further, you should check to see if the company the contact belongs to doesn’t already exist in your Salesforce database. There is no sense in having duplicate unique accounts for the same company.
Several methods are available in achieving this. The method I’ve implemented (not shown here) is checking the email domain from the contact’s email address and matching it to the domain of the company. I should point out that this was possible after creating a custom Salesforce object that maps the many domains which can be owned by a company.
$this->account_id = $response[0]->id);
should be:
$this->account_id = $response[0]->id;
in create_contact() function
Thanks Ray! That was a good catch! Never saw that extra parenthesis.
thanks ahmeddirie
Hi Ahmeddirie,
This is great help for learner/beginners.. Keep posting and enjoy life…
Regards
-Mahendra Singh
Hi
“SELECT Id,Name,MODEL__c, Brand__c ,Brand__r.name ,Product_Model__r.Id, Product_Model__r.name, Product_Description__c, Location__r.id, default_service_provider__c from Equipment_at_Store__c limit 2″
I run this query with below code
$response = $mySforceConnection->query($query);
$queryResult = new QueryResult($response);
if ($queryResult->size > 0) {
$records = $queryResult->records;
//print_r($records);
echo “”;
foreach ($queryResult->records as $record)
{
$sObject = new SObject($record);
/*echo “”;
//echo “”;
print_r($sObject);
echo “”;* /
$name= $record->Equipment_at_Store__c->Name;
$model= $sObject->fields->MODEL__c;
$brand_id= $sObject->fields->Brand__c;
$product_description= $sObject->fields->Product_Description__c;
$default_provider = $sObject->fields->Default_Service_Provider__c;
foreach($sObject->Brand__r as $item)
{
$brandname= $item->Name;
}
$post_data = array(‘product_desc’ => $product_description,
‘brand_name’ => $brandname,
‘model_name’ => $model,
‘serial_no’ => ‘234567892345’,
‘default_service_provider_id’ => $default_provider,
‘brand_id’ => $brand_id,
‘Name’ => $name
);
$alldata = json_encode($post_data);
print_r($alldata);
}
}
But it doenot return __r columns and __c properly how can I get these data in PHP code.
I can see entire record in Last_response object , and i believe we can’t use to get data.
Any idea??
Thanks
Mahendra Singh