Close

September 28, 2018

Add Email to Data Extension in Salesforce Using PHP

We were recently tasked to switch a data connection that was pushing contacts to Constant Contact, to a new Salesforce account. At first we found a good deal of documentation regarding their API and even PHP examples, however we soon found that much of it was outdated and not very clear as to the best approach to take.

We figured it out and will provide step-by-step instructions in this article.

Salesforce Account Setup

1.) You’ll need to setup a client id and client secret in Salesforce first.
2.) Login to Salesforce, then go to your name->Administration. Then under the account drop down, “Installed Packages”.
3.) Create a new one and call it whatever you would like. Then “Add Component” at the bottom.
4.) To move data into a Salesforce Data Extension, you’ll just need the “API Integration” component. Hit Next.
5.) Select the checkboxes for Read/Write in the “Data Extension” section.
6.) Your component should then give you a “client id” and “client secret”.
7.) Setup a data extension and keep the name handy.

PHP Code Setup:

This is where the biggest confusion came about. We tried a few of their PHP soap examples and eventually found that their FuelSDK-PHP kit works perfectly. The documentation on its use is not correct in Salesforce as you have to change the references to “ET_” instead of “SalesForce”.

1.) Go to Github and install the FuelSDK.
https://github.com/salesforce-marketingcloud/FuelSDK-PHP
2.) Setup a basic “driver” page to add emails.

<?php
    require(realpath(dirname(__FILE__).'/../../includes/salesforce/vendor/autoload.php'));
	use FuelSdk\ET_Client;
	use FuelSdk\ET_DataExtension;
	use FuelSdk\ET_DataExtension_Column;
	use FuelSdk\ET_DataExtension_Row;
 
	try {
	  $DataExtension="production_subscribers";
	  $myclient = new ET_Client(true,false,array("clientid" => "YOURCLIENTID", "clientsecret"=>"YOURCLIENTSECRET"));
 
	// Add a row to a DataExtension 
	  #print_r("Add a row to a DataExtension  \n");
	  $postDRRow = new ET_DataExtension_Row();
	  $postDRRow->authStub = $myclient;
	  $postDRRow->props = array("Email" => $inEmail);
	  $postDRRow->Name = $DataExtension;  
	  $postResult = $postDRRow->post();
 
	/* debug stuff
	  print_r('Post Status: '.($postResult->status ? 'true' : 'false')."\n");
	  print 'Code: '.$postResult->code."\n";
	  print 'Message: '.$postResult->message."\n";  
	  print 'Result Count: '.count($postResult->results)."\n";
	  print 'Results: '."\n";
	  print_r($postResult->results[0]);
	 */
	  print "\n---------------\n";
 
 
	  //Mr. Some Ting Wong
	  if ((!$postResult->status)&& ($postResult->results[0]->StatusCode=='Error') ) {
	      $err=$postResult->results[0]->ErrorMessage;
	      if(stristr($err, "Violation of PRIMARY KEY constraint")!== false){
	        #print "DEBUG: Plain violation key error..yawn";
	      }else{
	        print "\n Bad error dectedcted".$err;
	      }
	  }else{
	  	print "\n** New!  Added to Salesforce ($inEmail) **";
	  }
	}
	catch (Exception $e) {
	    echo 'Caught exception: ',  $e->getMessage(), "\n";
	}
 
}
?>

3.) That should be enough to get you started. For more complicated events/processes take a look at Salesforce’s PHP code:
https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-apis.meta/mc-apis/php_code_samples.htm