S
S
Sergey2020-11-17 14:20:43
API
Sergey, 2020-11-17 14:20:43

How to catch the onCrmCompanyAdd event in Bitrix24?

How to catch the onCrmCompanyAdd event in Bitrix24 so that the handler is in init.php?

In the handler, it is necessary to check the TIN of the new company for the presence of "duplication" with existing ones. If there is dubbing, then create a task "for manual dubbing check".

The problem is to catch the event of adding a company.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Nikolaev, 2020-11-18
@sslion

Note to the code below: if you have a company TIN field (custom, UF_*), then in the handleCompanyAdd method you should check it for uniqueness and already perform the action you need.
If the TIN is the details field, then register the agent, since at the time of the company's creation, the details may not yet be saved.

/**
 * Получаем объект менеджера событий.
 * Нужен для подписки на событие
 * @var Main\EventManager
 */
$eventManager = \Bitrix\Main\EventManager::getInstance();

/**
 * Подписка на событие "после создании компании"
 * постфикс Compatible нужен потому что событие из старого ядра 
 */
$eventManager->addEventHandlerCompatible(
  "crm",
  "OnAfterCrmCompanyAdd",
  [
    CompanyHandler::class,
    "handleCompanyAdd"
  ]
);

class CompanyHandler
{
  /**
   * Обработчик события crm::OnAfterCrmCompanyAdd
   * @param array &$arFields 
   * @return boolean
   */
  public static function handleCompanyAdd( &$arFields )
  {
    /**
     * Тут создаешь агента для этой компании
     * См. примечание
     */
    return true;
  }
}

S
Sergey, 2020-12-08
@sslion

I had to work out two events, for creating a company and for changing.
Unfortunately, only the company change event is correctly processed, but when creating, as it was said in the comment above, the company does not yet have a TIN, and accordingly the TIN check does not pass, although the company was specially made with "dubbing" ...
Maybe you still need to Is there a different way to check? (TIN is in the details of the company)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question