向 SuiteCRM 添加潜在客户时如何防止重复?

How Do I Prevent Duplicates When Adding Leads to SuiteCRM?

developer guide for SuiteCRM is kind of incomplete (at least here in Q4 2017) compared to the old SugarCRM one that it was based on before the software fork. So, by downloading the WordPress Plugin for SugarCRM,我能够使用以下代码找出 REST API 和 JSON 以将销售线索添加到 SuiteCRM。

现在如何防止家庭 phone、手机 phone 或电子邮件地址重复?

<?php

error_reporting(E_ALL);
ini_set('display_errors','On');

header('Content-Type: text/plain');

CRM::loginCRM('admin','xxxxPASSWORDxxxxxx');

$aResult = CRM::addLead(array(
  'name' => 'John Doe',
  'description' => 'sample description',
  'salutation' => 'Mr.',
  'first_name' => 'John',
  'last_name' => 'Doe',
  'do_not_call' => 'No',
  'phone_home' => '202-111-2222',
  'phone_mobile' => '202-111-2222',
  'email1' => 'test@example.com',
  'primary_address_street' => '123 Main Street',
  'primary_address_street2' => '',
  'primary_address_street3' => '',
  'primary_address_city' => 'New York',
  'primary_address_state' => 'NY'
));

print_r($aResult);
CRM::logoutCRM();


die('OK');
/////////////////////////

class CRM {

private static $SessionID = '';

private static $URL = 'https://mycrmserver-example.com/service/v4_1/rest.php';

private static $User = '';
private static $Shadow = '';

public static function sendJSON($a) {
  $s = file_get_contents(
    self::$URL,
    false,
    stream_context_create(
      array(
        'http' => array (
          'method' => 'POST',
          'header' => 'Content-Type: application/x-www-form-urlencoded',
          'content' => http_build_query($a)
        )
      )
    )
  );
  $a2 = json_decode($s);
  return $a2;
}

public static function loginCRM($sUser,$sPass) {
  $sShadow = md5($sPass);
  self::$User = $sUser;
  self::$Shadow = $sShadow;
  $asLogin = array (
    'method' => 'login',
    'input_type' => 'JSON',
    'response_type' => 'JSON',
    'rest_data' => json_encode(array(
      'user_auth' => array(
        'user_name' => $sUser,
        'password' => $sShadow,
        'version' => 1
      ),
      'application_name' => 'RestTest',
      'name_value_list' => array()
    ))
  );
  $a = self::sendJSON($asLogin);
  self::$SessionID = $a->id;
}

public static function logoutCRM() {
  $asLogin = array (
    'method' => 'logout',
    'input_type' => 'JSON',
    'response_type' => 'JSON',
    'rest_data' => json_encode(array(
      'user_auth' => array(
        'user_name' => self::$User,
        'password' => self::$Shadow,
        'version' => 1
      ),
      'application_name' => 'RestTest',
      'name_value_list' => array()
    ))
  );
  self::sendJSON($asLogin);
}

public static function addLead($a) {
  $asNameValueList = array();
  foreach($a as $sKey => $sVal) {
    $asNameValueList[] = array('name'=>$sKey,'value'=>$sVal);
  }
  $asAddEntry = array (
    'method' => 'set_entry',
    'input_type' => 'JSON',
    'response_type' => 'JSON',
    'rest_data' => json_encode(array(
      'session' => self::$SessionID,
      'module_name' => 'Leads',
      'name_value_list' => $asNameValueList
    ))
  );
  $a = self::sendJSON($asAddEntry);
  return $a;
}

} // end CRM

将这些功能添加到您的 CRM class 并在添加潜在客户之前检查它们。我从 this answer 那里得到了一点帮助,顺便给了我一些见解。此外,我建议您采取一些措施来加强安全性,例如添加 .htaccess 或 NGINX 规则,该规则仅允许某些 IP 地址或需要某些 headers 才能访问 /service 文件夹和 /service/* 子文件夹中的任何内容通过 HTTP 或 HTTPS。

public static function leadExistsByPhone($sHomePhone,$sMobilePhone) {
  $sHomePhone = (empty($sHomePhone)) ? 'xxxxxinvalid' : $sHomePhone;
  $sMobilePhone = (empty($sMobilePhone)) ? 'xxxxxinvalid' : $sMobilePhone;
  $asCheck = array (
    'method' => 'get_entry_list',
    'input_type' => 'JSON',
    'response_type' => 'JSON',
    'rest_data' => json_encode(array(
      'session' => self::$SessionID,
      'module_name' => 'Leads',
      'query' => "
        leads.phone_home = '$sHomePhone'
        OR leads.phone_mobile = '$sMobilePhone'
      ",
      'order_by' => 'leads.date_entered DESC',
      'offset' => '0',
      'select_fields' => array(),
      'link_name_to_fields_array' => array(),
      'max_results' => 999999,
      'deleted' => false
    ))
  );
  $a = self::sendJSON($asCheck);
  $nCount = @ $a->result_count;
  $nCount = intval($nCount);
  return ($nCount > 0);
}

public static function leadExistsByEmail($sEmail) {
  if (!filter_var($sEmail, FILTER_VALIDATE_EMAIL)) {
    die('DENIED: invalid email address format');
  }
  $asCheck = array (
    'method' => 'get_entry_list',
    'input_type' => 'JSON',
    'response_type' => 'JSON',
    'rest_data' => json_encode(array(
      'session' => self::$SessionID,
      'module_name' => 'Leads',
      'query' => "
        leads.id IN
        (
          SELECT email_addr_bean_rel.bean_id
          FROM   email_addr_bean_rel
          JOIN   email_addresses
          ON     email_addr_bean_rel.email_address_id = email_addresses.id
          WHERE
          email_addresses.email_address = '$sEmail'
        )
      ",
      'order_by' => 'leads.date_entered DESC',
      'offset' => '0',
      'select_fields' => array(),
      'link_name_to_fields_array' => array(),
      'max_results' => 999999,
      'deleted' => false
    ))
  );
  $a = self::sendJSON($asCheck);
  $nCount = @ $a->result_count;
  $nCount = intval($nCount);
  return ($nCount > 0);
}