• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/tracking/entity.php
  • Класс: Bitrix\Crm\Tracking\Entity
  • Вызов: Entity::onRestAfterUpdate
static function onRestAfterUpdate($entityTypeId, $entityId, array $fields)
{
	if (!$entityTypeId || !$entityId)
	{
		return;
	}

	$allowedTypes = [
		\CCrmOwnerType::Lead, \CCrmOwnerType::Contact, \CCrmOwnerType::Company,
		\CCrmOwnerType::Deal, \CCrmOwnerType::Quote,
	];
	if (!in_array($entityTypeId, $allowedTypes, true))
	{
		return;
	}

	self::processRestUtm($fields);

	$traceId = null;
	if (!empty($fields['TRACE']) && is_numeric($fields['TRACE']))
	{
		$traceId = (int) $fields['TRACE'];
	}

	if (!$traceId)
	{
		$trace = Tracking\Trace::create($fields['TRACE'] ?? null);
		foreach (Crm\UtmTable::getCodeList() as $utmCode)
		{
			if (!empty($fields[$utmCode]))
			{
				$trace->addUtm($utmCode, $fields[$utmCode]);
			}
		}

		if ($trace->getSourceId())
		{
			$previousTraceData = Tracking\Internals\TraceTable::getTraceByEntity($entityTypeId, $entityId);
			if (!$previousTraceData || $previousTraceData['SOURCE_ID'] != $trace->getSourceId())
			{
				$traceId = $trace->save();
			}
		}
	}

	if ($traceId)
	{
		Tracking\Trace::appendChannel($traceId, new Tracking\Channel\Rest());
		Tracking\Trace::appendEntity($traceId, $entityTypeId, $entityId);
	}
}