function sendSalesforceNotification() { if ($this->formrow->salesforce_enabled != 1) { return; } define("BF_SOAP_CLIENT_BASEDIR", JPATH_SITE . DS . 'administrator' . DS . 'components' . DS . 'com_breezingforms' . DS . 'libraries' . DS . 'salesforce'); if (!class_exists('SforcePartnerClient')) { require_once BF_SOAP_CLIENT_BASEDIR . '/SforcePartnerClient.php'; } if (!class_exists('SforceHeaderOptions')) { require_once BF_SOAP_CLIENT_BASEDIR . '/SforceHeaderOptions.php'; } try { $mySforceConnection = new SforcePartnerClient(); $trunc = new AllowFieldTruncationHeader(true); $mySforceConnection->setAllowFieldTruncationHeader($trunc); $mySoapClient = $mySforceConnection->createConnection(BF_SOAP_CLIENT_BASEDIR . '/partner.wsdl.xml'); $mylogin = $mySforceConnection->login($this->formrow->salesforce_username, $this->formrow->salesforce_password . $this->formrow->salesforce_token); $sobjects = $mySforceConnection->describeSObject($this->formrow->salesforce_type)->fields; $fields = array(); $this->formrow->salesforce_fields = explode(',', $this->formrow->salesforce_fields); foreach ($this->formrow->salesforce_fields as $sfields) { foreach ($this->sfdata as $savedata) { $sfield = explode('::', $sfields); if ($sfield[0] == $savedata[1]) { foreach ($sobjects as $sobject) { // forcing some primitives if ($sobject->name == $sfield[1]) { switch ($sobject->type) { case 'boolean': $savedata[4] = $savedata[4] ? 1 : 0; break; case 'int': $savedata[4] = intval($savedata[4]); break; case 'double': $savedata[4] = doubleval($savedata[4]); break; } break; } } $fields[$sfield[1]] = '<![CDATA[' . $savedata[4] . ']]>'; // bug in SF Toolkit appeareantly requires CDATA break; } } } $sObject = new SObject(); $sObject->fields = $fields; $sObject->type = $this->formrow->salesforce_type; $createResponse = $mySforceConnection->create(array($sObject)); } catch (Exception $e) { echo 'Salesforce Exception: ' . $e->getMessage(); session_write_close(); exit; } }
function PostSalesforce($awebreg) { // input: array $awebreg contains a range of form field data from some user-registration context // output: whatever you want it to be; this exmaple returns the LeadId of the inserted person, along with the CampaignId the lead was assigned to. ini_set("soap.wsdl_cache_enabled", "0"); // general SOAP wsdl caching - no caching? require_once 'salesforce/soapclient/SforcePartnerClient.php'; // Partner or Enterprise? Leroy provides this local WSDL require_once 'salesforce/soapclient/SforceHeaderOptions.php'; // Certain headers in a separate $wsdl = 'salesforce/soapclient/aigpartner.wsdl.xml'; // Shissler $userName = "******"; // Shissler $password = "******"; // I think this is built by requesting the key in the SF online wasteland. // Process of logging on and getting a salesforce.com session // Process of logging on and getting a salesforce.com session // Process of logging on and getting a salesforce.com session // Process of logging on and getting a salesforce.com session $client = new SforcePartnerClient(); // Partner or Enterprise? if ($client) { $csfresult .= writelog("+++ Connection Attempt START +++"); $client->createConnection($wsdl); $csfresult .= writelog("+++ Connection Attempt ENDED +++"); if ($client) { $csfresult .= writelog("+++ Login Attempt START, with u/p " . $userName . "/" . $password . " +++"); $loginResult = $client->login($userName, $password); $csfresult .= writelog("+++ Login Attempt ENDED +++"); if ($loginResult) { $areturn['login'] = parseObjectToString($loginResult); $csfresult .= writelog("*** login ***\n" . $areturn['login']); } else { $areturn['login'] = "******"; $csfresult .= writelog("#### LOGIN FAILED ####"); return $areturn; die; } } else { $areturn['login'] = "******"; $csfresult .= writelog("#### CREATECONNECTION FAILED ####"); return $areturn; die; } } else { $areturn['login'] = "******"; $csfresult .= writelog("#### sforcePartnerClient create FAILED ####"); return $areturn; die; } // Build up the transaction payload based on the incoming array of data fields // Build up the transaction payload based on the incoming array of data fields // Build up the transaction payload based on the incoming array of data fields // Build up the transaction payload based on the incoming array of data fields $records = array(); // Parse out Industry from the 3-level choice sent in from the form $aIndustry = explode("|", $awebreg['Industry']); // This is specific to old AS site, where a form returned a 3-level SF Industry code, showing the Industry/Sub/SF object id $thisIndustry = $aIndustry[0]; $records[0] = new SObject(); $records[0]->fields = array('FirstName' => htmlspecialchars($awebreg['FirstName']), 'LastName' => htmlspecialchars($awebreg['LastName']), 'Phone' => htmlspecialchars($awebreg['Phone']), 'Company' => htmlspecialchars($awebreg['Company']), 'Salutation' => htmlspecialchars($awebreg['salutation']), 'Title' => htmlspecialchars($awebreg['JobTitle']), 'Leadsource' => 'Web Direct', 'Email' => htmlspecialchars($awebreg['Useremail']), 'Street' => htmlspecialchars($awebreg['Address1']), 'City' => htmlspecialchars($awebreg['City']), 'State' => htmlspecialchars($awebreg['State']), 'PostalCode' => htmlspecialchars($awebreg['Zip']), 'Country' => htmlspecialchars($awebreg['Country']), 'HasOptedOutofEmail' => '0', 'Product_line__c' => 'Portable Analytical Instruments', 'Industry' => htmlspecialchars($thisIndustry), 'Markets_Application__c' => $awebreg['subIndustry'], 'Description' => htmlspecialchars("WebInquiry=" . $awebreg['HowBetterServed'] . ". Visitor was on webpage: " . $awebreg['WasOnPage']), 'RecordTypeId' => '012600000001BnZ'); $records[0]->type = 'Lead'; // Attempt the create $response = $client->create($records); $leadId = $response->id; // an ugly SF ID reference // now insert CampaignMember - new on 2014/08/03 $createFields = array('CampaignId' => $awebreg['CampaignId'], 'LeadId' => $leadId, 'Status' => 'Web To Lead'); $camprecords[0] = new SObject(); $camprecords[0]->fields = $createFields; $camprecords[0]->type = 'CampaignMember'; $createResp = $client->create($camprecords, 'CampaignMember'); $campaignId = $response->id; // I'm not sure this actually works, but it makes sense from the response of the Lead creation above. return array($leadId, $campaignId); }
} } // create an Account $acct = new sObject(); $acct->type = 'Account'; $fields = array(); $fields['Name'] = htmlspecialchars($_POST["company"], ENT_COMPAT, 'ISO-8859-1'); $fields['BillingStreet'] = htmlspecialchars($_POST["street"], ENT_COMPAT, 'ISO-8859-1'); $fields['BillingCity'] = htmlspecialchars($_POST["city"], ENT_COMPAT, 'ISO-8859-1'); $fields['BillingState'] = htmlspecialchars($_POST["state"] == "" ? $_POST["state2"] : $_POST["state"], ENT_COMPAT, 'ISO-8859-1'); $fields['BillingPostalCode'] = htmlspecialchars($_POST["zip"], ENT_COMPAT, 'ISO-8859-1'); $fields['BillingCountry'] = htmlspecialchars($_POST["country"], ENT_COMPAT, 'ISO-8859-1'); $fields['Phone'] = htmlspecialchars($_POST["phone"], ENT_COMPAT, 'ISO-8859-1'); $acct->fields = $fields; try { $response = $sforce->create(array($acct)); } catch (Exception $ex) { sendError($ex, $_POST["company"]); exit; } $acct->Id = $response->id; if ($log) { fwrite($log, date('c') . ' - Account: ' . $response->id . ' for ' . $_POST['company'] . "\n"); } // create a Contact $cont = new sObject(); $cont->type = 'Contact'; $fields = array(); $fields['AccountId'] = $acct->Id; $fields['FirstName'] = htmlspecialchars($_POST['first_name'], ENT_COMPAT, 'ISO-8859-1'); $fields['LastName'] = htmlspecialchars($_POST["last_name"], ENT_COMPAT, 'ISO-8859-1');
echo "Results of query '{$query}'<br/><br/>\n"; for ($queryResult->rewind(); $queryResult->pointer < $queryResult->size; $queryResult->next()) { $record = $queryResult->current(); // Id is on the $record, but other fields are accessed via // the fields object echo $record->Id . ": " . $record->fields->FirstName . " " . $record->fields->LastName . " " . $record->fields->Phone . "<br/>\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 $result->success == 1 ? $records[$i]->fields["FirstName"] . " " . $records[$i]->fields["LastName"] . " " . $records[$i]->fields["Phone"] . " created with id " . $result->id . "<br/>\n" : "Error: " . $result->errors->message . "<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';