Quantcast
Channel: WebDevJ » PHP
Viewing all articles
Browse latest Browse all 4

Salesforce Enterprise Client for posting leads

$
0
0

The other day one of our clients provided some Salesforce posting instruction that I was unfamiliar with. Normally our Salesforce clients use web2lead form code to receive leads. I was puzzled and made sure that’s the right instruction. Our client confirmed that they are on Salesforce Enterprise and web2lead is not available.

So I did my research and finally found out the following is working. This is provided that you downloaded the Salesforce Enterprise PHP Toolkit.

define('SALESFORCE_USER', 'user');
define('SALESFORCE_PASS', 'pass');
define('SALESFORCE_TOKEN', 'security_token');

define("SOAP_CLIENT_BASEDIR", "XXX/modules/salesforce/soapclient");
define('SALESFORCE_WSDL', SOAP_CLIENT_BASEDIR.'/enterprise.wsdl.xml');

require_once(SOAP_CLIENT_BASEDIR.'/SforceEnterpriseClient.php');

/**
 * explicitly turn off WSDL caching
 * there is a bug in PHP with this setting in php.ini
 * http://bugs.php.net/bug.php?id=41665
 * so it's safest to set it in the PHP source
 *
 * This param should take an int, not a string like most examples use
 * see http://us3.php.net/manual/en/soap.configuration.php
*/
ini_set('soap.wsdl_cache_enabled', 0);

// instantiate a new Salesforce Enterprise object
$crmHandle = new SforceEnterpriseClient();

// instantiate a SOAP connection to Salesforce
try {
  $crmHandle->createConnection(SALESFORCE_WSDL);
} catch (Exception $e) {
  // handle exception - did you set the WSDL path above?
  // we may also be in a Salesforce outage right now
}

$failed_login = 0;

// log in to Salesforce
try {
  $crmHandle->login(SALESFORCE_USER, SALESFORCE_PASS . SALESFORCE_TOKEN);
} catch (Exception $e) {
  // handle exception - did you modify the credentials above to your own?
	echo 'Message: ' .$e->getMessage();
	$failed_login = 1;
}

// create our lead
$lead = array();
$lead['Lead_Source__c'] 		= 'XXX';
$lead['Lead_ID__c'] 				= $leadid;
$lead['FirstName'] 					= $FIRSTNAME;
$lead['LastName'] 					= $LASTNAME;
$lead['Mailing_State__c'] 	= $STATE;
$lead['HOME_PHONE__c'] 			= $HOMEPHONE;
$lead['PHONE'] 							= $WORKPHONE;
$lead['MAILING_STREET__c'] 	= $ADDRESS;
$lead['MAILING_CITY__c'] 		= $CITY;
$lead['MAILING_POSTAL_CODE__c'] = $ZIP;
$lead['Email__c']						=	$EMAIL;
$lead['Lead_Loan_Amount__c']	=	$LOANAMOUNT;

if (!$failed_login) {

	// create the lead
	// $lead must be wrapped in an array, as create() can create
	// many objects with a single API call
	try {
		$result = $crmHandle->create(array($lead), 'Account');
	} catch (Exception $e) {
		//error
	}

	$response = print_r($result[0], true);

	if ($result[0]->success == 1)
		// lead accepted
	else { } // lead rejected 

	if ($testpost) {
		print_r($lead);
		print "\n\n";
		print_r($result[0]);
		print "\n\n";
		print $result[0]->success;
		print "\n\n";
	}
}

Viewing all articles
Browse latest Browse all 4

Trending Articles