Current Path :
/
home
/
zrcdevco
/
www
/
zrc-email-program
/
Or
Select Your Path :
Upload File :
New :
File
Dir
/home/zrcdevco/www/zrc-email-program/saved_search_ns_hs_search_test.php
<?php /************************************************* * * Scan the records to find the following: * - Open Records * - Click Records that did not unsubscribe. * send these records to Hubspot CRM. * Fields for Hubspot * - company * - email * - first name * - last name * - phone * - website * to Store other attributes such as project, project role, ... we will need to create a Custom Object in Hubspot * https://developers.hubspot.com/docs/api/crm/crm-custom-objects * @version 0.0.2 * @package Create Hubspot Contacts from active Netsuite Leads (people who click on an email) * @license GNU GPL * First test version to create Contacts in Hubspot Test account. * 0.0.1 Corrected the JavaScript reference location * changes to use the private app token_get_all * 0.0.2 Updated to pull database values and store Netsuite interactions * - email clicks and opens *************************************************/ // Configure NetSuite PHP API and Objects require_once 'PHPToolkit/NetSuiteService.php'; $service = new NetSuiteService(); $search = new CustomerSearchAdvanced(); // Array to set Project Role to a text field - this is a custom field in NetSuite $project_role = array("Architect"=>"1", "Structural Engineer"=>"2", "General Contractor"=>"3", "Structural Steel"=>"4"); // Configure HubSpot PHP API and objects require_once __DIR__.'/vendor/autoload.php'; use HubSpot\Factory; use HubSpot\Client\Crm\Contacts\ApiException; $client = Factory::createWithAccessToken("pat-na1-e33479fd-da09-4d3f-b7ba-883334bb0751"); $contactInput = new \HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInput(); use HubSpot\Client\Crm\Contacts\Model\Filter; use HubSpot\Client\Crm\Contacts\Model\FilterGroup; use HubSpot\Client\Crm\Contacts\Model\PublicObjectSearchRequest; $values1 = [ 'jeff@archconarchitecture.com' ]; $Filter1 = new Filter([ 'value' => 'string', 'values' => $values1, 'propertyName' => 'email', 'operator' => 'EQ' ]); $filters1 = [ $Filter1, ]; $FilterGroup1 = new FilterGroup([ 'filters' => $filters1 ]); $filterGroups1 = [ $FilterGroup1, ]; $sorts1 = 'string'; $properties1 = 'string'; $PublicObjectSearchRequest = new PublicObjectSearchRequest([ 'filterGroups' => $filterGroups1, 'sorts' => $sorts1, 'query' => string, 'properties' => $properties1, 'limit' => 0, 'after' => 0, ]); try { $apiResponse = $client->crm()->contacts()->searchApi()->doSearch($PublicObjectSearchRequest); var_dump($apiResponse); } catch (ApiException $e) { echo "Exception when calling search_api->do_search: ", $e->getMessage(); } // retrieve Netsuite Saved Search with Search ID of 313 // This search retrieve all contacts that have opened or clicked on email links $search->savedSearchId = "313"; $request = new SearchRequest(); $request->searchRecord = $search; $searchResponse = $service->search($request); // Error check for the search results if (!$searchResponse->searchResult->status->isSuccess) { echo "SEARCH ERROR"; //print_r($searchResponse); } else { echo "SEARCH SUCCESS, records found: " . $searchResponse->searchResult->totalRecords . "\n"; $records = $searchResponse->searchResult->searchRowList->searchRow; // Open a text file to store temporary data // Add a date stamp to the file name $processDate = date("Y-m-d"); $fileDate = str_replace('-','',$processDate); $myFile = fopen("newlead.".$fileDate.".txt", "w") or die("Unable to open file!"); // Step through each record - NS Contact foreach ($records as $record) { //print_r($record); echo "Name: " . $record->basic->altName[0]->searchValue . "\n"; echo "Campaign Response: " . $record->campaignResponseJoin->response[0]->searchValue . "\n"; $response = $record->campaignResponseJoin->response[0]->searchValue; // check for response if ($response == "_opened") { // For users that Open and Email output search results + // Lookup Company Name, Project Name, Project Role, Project Price, LEED Certification // use this program to lookup company name, project name and project role. // ProjectRole (custentityprojectrole) is always the [3] element in the customField Array - this will also need to lookup the Role list // Project Name (custentityprojectname) is always the [6] element in the customField Array - we can pull the searchValue object element, $contactInput = new \HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInput(); // configure COntact Data $name = $record->basic->altName[0]->searchValue; $name = explode( ',', $record->basic->altName[0]->searchValue); $firstname = array_pop($name); $lastname = array_shift($name); $contactInput->setProperties([ "company" => $record->basic->companyName[0]->searchValue, "email" => $record->basic->email[0]->searchValue, "firstname" => $firstname, "lastname" => $lastname, "phone" => $record->basic->phone[0]->searchValue, "website" => array_pop(explode( '@', $record->basic->email[0]->searchValue)), "project_name___cmd" => $record->basic->customFieldList->customField[6]->searchValue, "project_role" => array_search($record->basic->customFieldList->customField[3]->searchValue->internalId, $project_role), "leed_project" => $record->basic->customFieldList->customField[8]->searchValue->internalId ]); print_r($contactInput); // Need to add a check for an existing contact // to determine whether to create or update the contact // $contact = $client->crm()->contacts()->basicApi()->create($contactInput); // // we are not saving project price yet // https://www.trionia.com/ZRC/CMDTest/NS-EmailProgram/contact_search_ver1.php $txt=""; // initialize output // first name (end of the name string) $txt = $firstname.","; // last (start of the name string) $txt .= $lastname.","; $txt .= $record->basic->email[0]->searchValue.","; $txt .= $record->basic->phone[0]->searchValue.","; // website URL (end of the email string) $txt .= array_pop(explode( '@', $record->basic->email[0]->searchValue)).","; // Property Role $role = array_search($record->basic->customFieldList->customField[3]->searchValue->internalId, $project_role); // $key = 2; $txt .= $role.","; // Property Name $txt .= $record->basic->customFieldList->customField[6]->searchValue."\n"; echo($txt); fwrite($myFile, $txt); } } fclose($myFile); } ?>