S
S
Sergey Ch2017-07-31 08:41:17
1C-Bitrix
Sergey Ch, 2017-07-31 08:41:17

Bitrix CRM Get contact ID?


What is the method in the boxed version
of Bitrix 24 to get the contact ID or how to filter from the general list of contacts if its full name is known in the
system
: ]=>
string(20) "Patronymic name"
["CONTACT_LAST_NAME"]=>
string(16) "Last name"
["CONTACT_FULL_NAME"]=>
string(29) "Full name"
["CONTACT_POST"]=>
string(0) ""
["CONTACT_ADDRESS"]=>
string(78) "Address"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Nikolaev, 2017-07-31
@elov4anin

There are at least 2 options (without direct queries to the database) how to do this:
1) High-level (always works). Backwards compatible method: CCrmContact::GetList ( bxapi.ru/src/?id=183244)
Application (based on the code described by Artem ):

/* @var array Список контактов */
$arContacts = array();

if ( \Bitrix\Main\Loader::IncludeModule('crm') )
{
  /* @var array Сортировка полученного списка контактов */
  $arOrder  = array('ID' => 'DESC');

  /* @var array Условия получаемого списка контактов */
  $arFilter = array(
    "NAME"      => "Имя",
    "LAST_NAME" => "Фамилия",
    'CHECK_PERMISSIONS' => 'N' // Данный ключ необходим для того чтобы получить всех пользоватей,
                   // иначе, будет найден только если ответственным за него является тот,
                   // под кем запускается скрипт в битриксе
  );

  /* @var array Получаемые поля для списка контактов */
  $arSelect = array(
    'ID'
  );

  // NOTE: Запрашивайте только необходимые поля
  $res = CCrmContact::GetList( $arOrder, $arFilter, $arSelect );

  while( $arContact = $res->fetch() )
  {
    $arContacts[ $arContact['ID'] ] = $arContact['ID'];
  }
}

// Тут в $arContacts либо пустой массив, либо массив с ID контактами, которые соответствуют условию поиска

2) Low-level DataMapper (new d7 approach)
use \Bitrix\Main\Loader;
use \Bitrix\Crm;

/* @var array Список контактов */
$arContacts = array();

if ( Loader::IncludeModule('crm') )
{

  $resContacts = Crm\ContactTable::getList(array(
    'select' => array('ID'),
    'filter' => array(
      "NAME"      => "Имя",
      "LAST_NAME" => "Фамилия",
    ),
    'order' => array('ID' => 'DESC')
  ));

  while( $arContact = $resContacts->fetch() )
  {
    $arContacts[ $arContact['ID'] ] = $arContact['ID'];
  }

  /*
  Начиная с 17 версии (вроде бы), можно делать так:
  foreach( $resContacts as $arContact)
  {
    $arContacts[ $arContact['ID'] ] = $arContact['ID'];
  }
  вместо while цикла
  */
}

Advantage of the first approach:
- It always works, both for new and old projects
- You can use access levels
Advantage of the new approach:
- Technologically universal due to d7 (you can add connections at runtime, get related entities, etc.)
- In DataMapper is a caching technology, i.e. you can cache the result simply by adding another key to the getList array
- Subjectively, the code is easier to perceive

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question