CCrmDocumentDeal::CreateDocument

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmDocumentDeal
  4. CreateDocument
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_document_deal.php
  • Класс: \CCrmDocumentDeal
  • Вызов: CCrmDocumentDeal::CreateDocument
static function CreateDocument($parentDocumentId, $arFields)
{
	if(!is_array($arFields))
	{
		throw new Exception('Entity fields must be array');
	}

	global $DB;
	$arDocumentID = self::GetDocumentInfo($parentDocumentId);
	if ($arDocumentID == false)
	{
		$arDocumentID['TYPE'] = $parentDocumentId;
	}

	$arFields = self::performTypeCast($arDocumentID, $arFields);
	$arFields = self::performTypeCast4CategoryAndStage($arFields);

	$useTransaction = static::shouldUseTransaction();

	if ($useTransaction)
	{
		$DB->StartTransaction();
	}

	$CCrmEntity = new CCrmDeal(false);
	$id = $CCrmEntity->Add(
		$arFields,
		true,
		[
			'DISABLE_USER_FIELD_CHECK' => true,
			'REGISTER_SONET_EVENT' => true,
			'CURRENT_USER' => static::getSystemUserId(),
		]
	);

	if (!$id || $id <= 0)
	{
		if ($useTransaction)
		{
			$DB->Rollback();
		}
		throw new Exception($CCrmEntity->LAST_ERROR);
	}

	if (isset($arFields['TRACKING_SOURCE_ID']))
	{
		Crm\Tracking\UI\Details::saveEntityData(\CCrmOwnerType::Deal, $id, $arFields);
	}

	if (COption::GetOptionString('crm', 'start_bp_within_bp', 'N') == 'Y')
	{
		$CCrmBizProc = new CCrmBizProc('DEAL');
		if (false === $CCrmBizProc->CheckFields(false, true))
		{
			if ($useTransaction)
			{
				$DB->Rollback();
			}
			throw new Exception($CCrmBizProc->LAST_ERROR);
		}

		if ($id && $id > 0 && !$CCrmBizProc->StartWorkflow($id))
		{
			if ($useTransaction)
			{
				$DB->Rollback();
			}
			throw new Exception($CCrmBizProc->LAST_ERROR);
		}
	}

	//region automation
	$starter = new Crm\Automation\Starter(\CCrmOwnerType::Deal, $id);
	$starter->setContextToBizproc()->runOnAdd();
	//endregion

	if ($id && $id > 0 && $useTransaction)
	{
		$DB->Commit();
	}

	return $id;
}

Добавить комментарий