{"id":3870,"date":"2013-05-30T20:04:40","date_gmt":"2013-05-31T01:04:40","guid":{"rendered":"http:\/\/ahmeddirie.com\/?p=3870"},"modified":"2013-06-20T21:04:59","modified_gmt":"2013-06-21T02:04:59","slug":"salesforce-php-soap-api-creating-contacts-accounts-and-associating-records","status":"publish","type":"post","link":"https:\/\/ahmeddirie.com\/blog\/web-development\/salesforce-php-soap-api-creating-contacts-accounts-and-associating-records\/","title":{"rendered":"Salesforce PHP SOAP API &#8211; Creating Contacts, Accounts and Associating Records"},"content":{"rendered":"<p>When working with Salesforce&#8217;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.<\/p>\n<p><!--more--><\/p>\n<p>In the case of Salesforce and creating new contacts and a company (account) for the contact, you&#8217;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.<\/p>\n<p>In previous examples such as <a href=\"\/technology\/web-development\/getting-record-data-from-salesforce-soap-api-using-php\/\" title=\"Getting Record Data from Salesforce SOAP API using PHP\">this post<\/a>, I&#8217;ve used an include file containing the Salesforce Connection parameters and some procedural PHP for passing or retrieving data. In this example, I&#8217;ll use a Salesforce object that includes the connection, as well as the functions for the various operations we&#8217;re performing.<\/p>\n<h2>Creating the Salesforce Class<\/h2>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nclass Salesforce {\r\n\t\r\n\t\/\/Contact Information\r\n\tvar $first_name\t\t= '';\r\n\tvar $last_name\t\t= '';\r\n\tvar $email_address\t= '';\r\n\tvar $company_name\t= '';\r\n\t\r\n\t\/\/Salesforce Contact and Account\r\n\tvar $contact_id\t\t= '';\r\n\tvar $account_id\t\t= '';\r\n\t\r\n\t\/\/Connection to Salesforce\r\n\tvar $connection\t\t= '';\r\n\t\r\n\t\/\/Constructor\r\n\tfunction __construct() {\r\n\t\t\/\/Initiation a connection to Salesforce\r\n\t\t$this-&gt;$connection = $this-&gt;connect_salesforce();\r\n\t}\r\n\t\r\n\t\/\/Connecting to Salesforce\r\n\tfunction connect_salesforce() {\r\n\t\ttry {\r\n\t\t\t$username = &quot;YOUR_USERNAME&quot;;\r\n\t\t\t$password = &quot;YOUR_PASSWORD&quot;;\r\n\t\t\t$security_token = &quot;YOUR_TOKEN&quot;;\r\n\t\t\t$wsdl = &quot;\/PATH_TO_PHP_TOOLKIT\/soapclient\/partner.wsdl.xml&quot;;\r\n\t\t\r\n\t\t\trequire_once ('PATH_TO_PHP_TOOLKIT\/soapclient\/SforcePartnerClient.php');\r\n\r\n\t\t\t$this-&gt;connection = new SforcePartnerClient();\r\n\t\t\t$this-&gt;connection-&gt;createConnection($wsdl);\r\n\t\t\t$this-&gt;connection-&gt;login($username, $password.$security_token);\r\n\t\t} catch (Exception $e) {\r\n\t\t\tthrow new Exception( 'Could not connect to Salesforce', 0, $e);\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<h2>Adding a Contact Function to the class<\/h2>\n<p>Now we&#8217;ve got the class created. We&#8217;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&#8217;re only working with the name and email address.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction create_contact() { \r\n\ttry {\r\n\t\t$record = array(\r\n\t\t\t'FirstName' =&gt; $this-&gt;first_name,\r\n\t\t\t'LastName' =&gt; $this-&gt;last_name,\r\n\t\t\t'Email' =&gt; $this-&gt;email_address\r\n\t\t);\r\n\t\t\r\n\t\t\/\/Creating the Contact\r\n\t\t$contact = new stdClass;\r\n\t\t$contact-&gt;type = 'Contact';\r\n\t\t$contact-&gt;fields = $record;\r\n\t\t$response = $this-&gt;connection-&gt;create(array($contact), 'Contact');\r\n\t\t\r\n\t\t\/\/Storing the Contact ID\r\n\t\t$this-&gt;contact_id = $response[0]-&gt;id;\r\n\t\t\r\n\t} catch (Exception $e) {\r\n\t\tthrow new Exception( 'Could not create a contact for '.$this-&gt;first_name.' '.$this-&gt;last_name, 0, $e);\r\n\t}\r\n}\r\n<\/pre>\n<h2>Adding an Account Function to the class<\/h2>\n<p>The previous function creates our contact record in the Contact object. We&#8217;ll now write another function that allows us to create a company in the Account object.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction create_account() { \r\n\ttry {\r\n\t\t$record = array(\r\n\t\t\t'Name' =&gt; $this-&gt;company_name\r\n\t\t);\r\n\t\t\r\n\t\t\/\/Creating the Company\r\n\t\t$company = new stdClass;\r\n\t\t$company-&gt;type = 'Account';\r\n\t\t$company-&gt;fields = $record;\r\n\t\t$response = $this-&gt;connection-&gt;create(array($company), 'Account');\r\n\t\t\r\n\t\t\/\/Storing the Account ID\r\n\t\t$this-&gt;account_id = $response[0]-&gt;id);\r\n\t\t\r\n\t} catch (Exception $e) {\r\n\t\tthrow new Exception( 'Could not create an account for '.$this-&gt;company_name, 0, $e);\r\n\t}\r\n}\r\n<\/pre>\n<h2>Adding a function that associates the Contact and the Account<\/h2>\n<p>Now you&#8217;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&#8217;s record.<\/p>\n<p>You&#8217;ll notice here that we&#8217;re not tracking the response code and we&#8217;re using the Update command instead of Create.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/Associate Account to the Contact record\r\nfunction associate_contact_account() { \r\n\ttry {\r\n\t\t\/\/Update the Contact with the Account\r\n\t\t$record = array(\r\n\t\t\t'Id' =&gt; $this-&gt;contact_id,\r\n\t\t\t'AccountId' =&gt; $this-&gt;account_id\r\n\t\t);\r\n\r\n\t\t\/\/Submitting the Account Id to the Contact Record\r\n\t\t$contact = new stdClass;\r\n   \t\t$contact-&gt;type = 'Contact';\r\n   \t\t$contact-&gt;fields = $record;\r\n\t\t$this-&gt;connection-&gt;update(array($contact), 'Contact');\r\n\t} catch (Exception $e) {\r\n\t\tthrow new Exception( 'Could not associate the contact to the account for '.$this-&gt;first_name.' '.$this-&gt;last_name, 0, $e);\r\n\t}\r\n}\r\n<\/pre>\n<h2>Working around duplicate Company records<\/h2>\n<p>We&#8217;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&#8217;t already exist in your Salesforce database. There is no sense in having duplicate unique accounts for the same company. <\/p>\n<p>Several methods are available in achieving this. The method I&#8217;ve implemented (not shown here) is checking the email domain from the contact&#8217;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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with Salesforce&#8217;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.<\/p>\n","protected":false},"author":2,"featured_media":3899,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[152],"tags":[51,288,453,22,55,331],"_links":{"self":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts\/3870"}],"collection":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/comments?post=3870"}],"version-history":[{"count":28,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts\/3870\/revisions"}],"predecessor-version":[{"id":3949,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts\/3870\/revisions\/3949"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/media\/3899"}],"wp:attachment":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/media?parent=3870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/categories?post=3870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/tags?post=3870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}