On this page
comment
Работа с комментариями сделок
Примеры работы с комментариями сделок в Битрикс24: перенос, получение и создание.
Перенос всех комментариев из одной сделки в другую
Этот PHP-код позволяет перенести все комментарии из одной сделки в другую в Битрикс24.
use Bitrix\Crm\Timeline\Entity\TimelineBindingTable;
use Bitrix\Crm\Timeline\TimelineType;
$entityTypeID = CCrmOwnerType::Deal; // Тип сущности (например, сделка)
$oldEntityID = 31495; // Текущий ID сущности
$newEntityID = 31496; // Новый ID сущности
TimelineBindingTable::rebind($entityTypeID, $oldEntityID, $newEntityID, [TimelineType::COMMENT]);
Получение всех комментариев таймлайна по исходной сделке и создание их в целевой сделке
Данный скрипт позволяет получить все комментарии из таймлайна одной сделки и создать их в другой целевой сделке.
use Bitrix\Crm\Timeline\CommentEntry;
use Bitrix\Crm\Timeline\Entity\TimelineBindingTable;
use Bitrix\Crm\Timeline\Entity\TimelineTable;
\Bitrix\Main\Loader::includeModule('crm');
$sourceDealId = $this->GetRootActivity()->GetVariable("SourceDealID");
$targetDealId = $this->GetRootActivity()->GetVariable("TargetDealID");
$authorId = $this->GetRootActivity()->GetVariable("AuthorID");
$commentsTransferredCount = 0;
$transferStatus = true;
$errorMessage = '';
if (!$sourceDealId || !$targetDealId || !$authorId) {
$errorMessage = "КРИТИЧНО: Не все обязательные параметры для переноса комментариев указаны.";
$this->WriteToTrackingService($errorMessage);
$transferStatus = false;
} else {
$obTimeLineEntity = TimelineTable::getList([
'order' => ['CREATED' => 'ASC'], // Сортируем по возрастанию, чтобы сохранить порядок
'filter' => [
'BINDINGS.ENTITY_ID' => $sourceDealId,
'!COMMENT' => false,
],
'select' => ['*'],
]);
$commentData = [];
while ($arFields = $obTimeLineEntity->fetch()) {
if (!empty($arFields['COMMENT'])) {
$commentData[] = $arFields;
}
}
foreach ($commentData as $comment) {
$commentFields = [
'TEXT' => $comment['COMMENT'],
'AUTHOR_ID' => $authorId,
'BINDINGS' => [
[
'ENTITY_TYPE_ID' => \CCrmOwnerType::Deal,
'ENTITY_ID' => $targetDealId,
],
],
];
$result = CommentEntry::create($commentFields);
if ($result->isSuccess()) {
$commentsTransferredCount++;
} else {
$transferStatus = false;
$this->WriteToTrackingService("ОШИБКА: Не удалось создать комментарий. " . implode(', ', $result->getErrorMessages()));
}
}
}
$rootActivity = $this->GetRootActivity();
$rootActivity->SetVariable("CommentsTransferredCount", $commentsTransferredCount);
$rootActivity->SetVariable("CommentsTransferStatus", $transferStatus);
$rootActivity->SetVariable("CommentsTransferErrorMessage", $errorMessage);
Получение всех комментариев таймлайна по сделке
Этот PHP-код позволяет получить все комментарии из таймлайна указанной сделки.
use Bitrix\Crm\Timeline\Entity\TimelineTable;
\Bitrix\Main\Loader::includeModule('crm');
$dealId = $this->GetRootActivity()->GetVariable("DealID");
$foundCommentsCount = 0;
$commentIDs = [];
$errorMessage = '';
if (!$dealId) {
$errorMessage = "КРИТИЧНО: Не указан ID сделки для получения комментариев.";
$this->WriteToTrackingService($errorMessage);
} else {
$obTimeLineEntity = TimelineTable::getList([
'order' => ['CREATED' => 'DESC'],
'filter' => [
'BINDINGS.ENTITY_ID' => $dealId,
'!COMMENT' => false,
],
'select' => ['ID'],
]);
while ($arFields = $obTimeLineEntity->fetch()) {
$commentIDs[] = $arFields['ID'];
}
$foundCommentsCount = count($commentIDs);
}
$rootActivity = $this->GetRootActivity();
$rootActivity->SetVariable("FoundCommentsCount", $foundCommentsCount);
$rootActivity->SetVariable("CommentIDs", implode(",", $commentIDs));
$rootActivity->SetVariable("CommentSearchErrorMessage", $errorMessage);