/* * This code sample contains several functions that work together to enable the efficient import of new (or update of existing) Contacts. * Contacts support several standard fields and unlimited custom fields. You'll find that the code in this sample handles these fields. * Each Contact is, by default, assigned to an Account (which can be essentially empty or undefined). The code in this sample handles Account standard and custom fields as well. */

/* * The few lines of code immediately below should be all that are required in order to successfully import Contacts if you are not using any custom fields. * If you're using custom fields you'll need to have a look at contactImportSetOne() function (which is pretty well commented). * * Each Contact you want imported is described by an array. Those contact arrays are in turn nested in a containing array (as in the example immediately below). * The functions in this sample will convert all of that into JSON and execute the appropriate API call (ContactImport::setOne) * * You may pass, potentially, a very large number of Contacts in a single call. * The actual number you may pass depends on the number of fields you're including (don't get too crazy with kilobyte size). * We can pass 10,000 or more Contacts in call if we're not passing lots of fields. * * The functions in this file are fairly well commented. Please contact our support team with any questions. We're happy to help :) */

function contactImportSetOne($arrContacts, $arrAddToLists = array(), $arrRemoveFromLists = array()) {/** * This is the main function that imports Contacts via API. The other functions in this sample are helpers that perform tasks that make this import easier. * * You pass to this function an array of Contacts. You may pass many contacts in one call. Depending on the number of fields you are passing for each Contact, you may be able to pass 10,000 Contacts in a single call. * Please do not call this method once for each Contact. You must batch your Contacts into a single call whenever possible. * * * This call is asynchronous. It is designed for situations where you may be importing many Contacts simultaneously. * It adds your Contacts to a job queue for import and returns to you the id of your job in that queue. * Included in this example is a function you can use to check if an import is complete. * * If you need to import a single Contact synchronously to return a contact_id (for example, when you want to import a single Contact then immediately email that Contact) use the getOrCreateContactIdByContactEmailAddress() method included in this example */

$intImportJobId = null;

//build mappings for standard fields (both Contact and Account) automatically...$arrContactStandardColumns = buildContactStandardFieldMap($arrContacts[0]); //this will build an array that "maps" the field names passed in your array of Contacts to their actual field names at the Contact level$arrAccountStandardColumns = buildAccountStandardFieldMap($arrContacts[0]); //this will build an array that "maps" the field names passed in your array of Contacts to their actual field names at the Account level

$arrParams = ['contact_import' => ['contact_import_id' => '', //leave this as an empty string. The API will return a contact_import_id upon success'contact_import_overwrite' => true, //true will allow the API to update existing Contacts if you pass in a Contact that already exists. false will cause the API to ignore Contacts in your array if that Contact already exists'ContactImportRecipient' => array('your_email@your_domain.com'), //optional. you may pass an empty array to opt-out of email notifications'ContactImportColumn' => $arrContactImportColumns, //the array we assembled previously that maps all standard and custom fields for Contacts and Accounts'ContactImportEmailListAssn' => $arrAddToLists, //optional, you may pass an empty array'ContactImportEmailListRemoveAssn' => $arrRemoveFromLists, //optional, you may pass an empty array'Contacts' => $arrContacts] ];

//we recommend logging all import jobs to a database table for auditing purposes. Here is where we insert $strJSONParams to a table}

return $intImportJobId;}

function buildContactStandardFieldMap($arrContact) {/** * This "maps" the field names you used in your array of Contacts to their actual field names. * This function deals only with "standard Contact fields". * * Standard fields for Accounts are handled via buildAccountStandardFieldMap(). * Custom fields for both Contacts and Accounts are handled via buildCustomFieldMap(). * * Look at the $arrStandardFields array below. See that the field called "email_address" from our Contacts array is mapped to the field called "Email Address". * You'll also see that this mapping array will handle that mapping whether you pass your email address as "email" or "emailAddress". * Modify the left side of this array as needed based on the names of the fields you pass in your array of Contacts */

function buildAccountStandardFieldMap($arrContact) {/** * This "maps" the field names you used in your array of Contacts to their actual field names. * This function deals only with "standard Account fields". * * Standard fields for Contacts are handled via buildContactStandardFieldMap(). * Custom fields for both Contacts and Accounts are handled via buildCustomFieldMap(). */

function checkContactImportCompletionStatusByContactImportId($intContactImportId) {/** * This function will check whether your queued job is complete or not and return to you a boolean true or false */

function getContactIdByContactEmailAddress($strEmail) {/** * This function simply executes the Contact::getContactIdByEmailAddress method in the API and returns that contact_id on success or false on failure. */

function getOrCreateContactIdByContactEmailAddress($strEmail) {/** * This function allows you to create a single contact "synchronously". * * We're using the asynchronous ContactImport::setOne method, but we're then checking several times for up to a minute or so to see if the import job queued up by the setOne method is complete. * If if is complete we use the getContactIdByContactEmailAddress() function to get our newly created Contact's email address. */