How to Create a Comment in a Task and Attach a File
Scope:
drive,tasksWho can execute the method: users with access to the drive and tasks sections
In Bitrix24, there are two types of file fields:
- File. This field is not linked to the drive; files are uploaded directly through the Base64 format string.
- File (drive). This field is linked to the drive, and it stores the ID of the drive object. The Base64 format is not processed in this field, so the file must first be uploaded to the Bitrix24 drive.
To create a comment in a task and attach a file, we will sequentially execute two methods:
- disk.folder.uploadfile — this method uploads a file to the drive.
- task.commentitem.add — this method creates a comment.
1. Uploading a File to the Bitrix24 Drive
To upload a file to the drive, we use the disk.folder.uploadfile method with the following parameters:
id— we specify the value1739— the identifier of the drive folder where we are uploading the file.data— we specify the file nameNAME, under which the file will be saved on the Bitrix24 drive.fileContent— we pass the file in the format ['file_name.extension', 'file as a Base64 encoded string'].
Uploading the file to the drive is a necessary step, as the UF_FORUM_MESSAGE_DOC field in comments only accepts the IDs of drive files.
How to Use Examples in Documentation
BX24.callMethod(
"disk.folder.uploadfile",
{
id: 1739,
data: {
NAME: "file.pdf"
},
fileContent: [
'file555.pdf',
'/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAQDAwQDAwQEAwQ///+dAYq6YFKoAv/AFnAa6ArKv8AAtFJVppxCEAulxQ2DWgfMR//2Q=='
]
}
);
require_once('crest.php');
$result = CRest::call(
'disk.folder.uploadfile',
[
'id' => 1739,
'data' => [
'NAME' => 'file.pdf'
],
'fileContent' => [
'file555.pdf',
'/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAQDAwQDAwQEAwQ///+dAYq6YFKoAv/AFnAa6ArKv8AAtFJVppxCEAulxQ2DWgfMR//2Q=='
]
]
);
As a result of uploading the file to the drive, we received two different file ID values:
FILE_ID:28073— the internal file ID value.ID:6687— the drive object ID; this value is used in methods for working with fields of the "file (drive)" type.
If the request to change the "file (drive)" field passes theFILE_IDvalue, the file will either not be attached to the task because there is no drive object with that ID, or the wrong file will be attached.
{
"result": {
"ID": 6687,
"NAME": "file.pdf",
"CODE": null,
"STORAGE_ID": "1",
"TYPE": "file",
"PARENT_ID": "1739",
"DELETED_TYPE": 0,
"GLOBAL_CONTENT_VERSION": 1,
"FILE_ID": 28073,
"SIZE": "405559",
"CREATE_TIME": "2024-11-01T17:00:55+02:00",
"UPDATE_TIME": "2024-11-01T17:00:55+02:00",
"DELETE_TIME": null,
"CREATED_BY": "1",
"UPDATED_BY": "1",
"DELETED_BY": null,
"DOWNLOAD_URL": "https://your-domain.bitrix24.com/rest/download.json?sessid=9dd90ed5a58ccc41af81f5f0043739db&token=disk%7CaWQ9NjY4NyZfPTJ5ZXdvN2Fsb09SMGw1b0FHTkRMSGR5MFJkN1pLTjNS%7CImRvd25sb2FkfGRpc2t8YVdROU5qWTROeVpmUFRKNVpYZHZOMkZzYjA5U01HdzFiMEZIVGtSTVNHUjVNRkprTjFwTFRqTlN8OWRkOTBlZDVhNThjY2M0MWFmODFmNWYwMDQzNzM5ZGIi.Lup1vDbibL6twiCPfCMFnLSoDLleNX0cfMHGv5PFaJw%3D",
"DETAIL_URL": "https://your-domain.bitrix24.com/company/personal/user/1/disk/file/Created files/New folder for testing the process/file.pdf"
}
}
2. Creating a Comment and Attaching a File
To create a comment in a task, we use the task.commentitem.add method with the following parameters:
TASKID— the ID of the task, a required field. Without the task ID, the comment will not be created. To obtain the task ID, we use the tasks.task.list method.AUTHOR_ID— the ID of the comment author. This parameter can be omitted, in which case the author will automatically be the employee from whose account the request is made.POST_MESSAGE— the text of the comment.UF_FORUM_MESSAGE_DOC— we specify the valuen6687. This is the drive file ID from the result of the previous method, to which we add the prefixnfor uploading the file to the field.
BX24.callMethod(
'task.commentitem.add',
{
TASKID: 3711,
FIELDS: {
POST_MESSAGE: 'comment for test',
AUTHOR_ID: 29,
UF_FORUM_MESSAGE_DOC: [
"n6687"
]
}
}
);
require_once('crest.php');
$result = CRest::call(
'task.commentitem.add',
[
'TASKID' => 3711,
'FIELDS' => [
'POST_MESSAGE' => 'comment for test',
'AUTHOR_ID' => 29,
'UF_FORUM_MESSAGE_DOC' => [
'n6687'
]
]
]
);
We created a comment with ID 9393.
{
"result": 9393
}
In the received result, there is no information about the file attached to the comment. To check if the file was successfully attached, we will execute the task.commentitem.get method.
Code Example
// Function to upload a file
function uploadFileToDisk() {
// ID of the folder to which the file needs to be uploaded
var folderId = 'folder_ID';
// File name and its content in Base64 format
var fileName = 'file_name';
var fileContentBase64 = 'file_content_Base64';
// Calling the disk.folder.uploadfile method
BX24.callMethod(
'disk.folder.uploadfile',
{
id: folderId,
data: {
NAME: fileName
},
fileContent: [
fileName,
fileContentBase64
]
},
function(result) {
if (result.error()) {
console.error('Error uploading file:', result.error());
} else {
console.log('File successfully uploaded!', result.data());
var fileId = result.data().ID; // Using ID from the result
createCommentWithFile(fileId);
}
}
);
}
// Function to create a comment with a file
function createCommentWithFile(fileId) {
// Comment parameters
var taskID = 'task_ID';
var commentMessage = 'comment_text';
var authorId = 'comment_author_ID';
// Calling the task.commentitem.add method
BX24.callMethod(
'task.commentitem.add',
{
TASKID: taskID,
FIELDS: {
POST_MESSAGE: commentMessage,
AUTHOR_ID: authorId,
UF_FORUM_MESSAGE_DOC: ['n' + fileId] // Adding prefix 'n' to the file ID
}
},
function(result) {
if (result.error()) {
console.error('Error creating comment:', result.error());
} else {
console.log('Comment successfully created!', result.data());
}
}
);
}
// Calling the function to upload a file and create a comment
uploadFileToDisk();
require_once('crest.php');
// Function to upload a file
function uploadFileToDisk() {
// ID of the folder to which the file needs to be uploaded
$folderId = 'folder_ID';
// Name of the file you want to upload
$fileName = 'file_name';
// Path to the file on your file system
$filePath = '/path/to/your/file';
// Reading the file content and encoding it in Base64
$fileContentBase64 = base64_encode(file_get_contents($filePath));
// Calling the disk.folder.uploadfile method
$result = CRest::call(
'disk.folder.uploadfile',
[
'id' => $folderId,
'data' => [
'NAME' => $fileName
],
'fileContent' => [
$fileName,
$fileContentBase64
]
]
);
if (isset($result['error'])) {
echo 'Error uploading file: ' . $result['error'];
} else {
echo 'File successfully uploaded!';
$fileId = $result['result']['ID']; // Using ID from the result
createCommentWithFile($fileId);
}
}
// Function to create a comment with a file
function createCommentWithFile($fileId) {
// Comment parameters
$taskID = 'task_ID';
$commentMessage = 'comment_text';
$authorId = 'comment_author_ID';
// Calling the task.commentitem.add method
$result = CRest::call(
'task.commentitem.add',
[
'TASKID' => $taskID,
'FIELDS' => [
'POST_MESSAGE' => $commentMessage,
'AUTHOR_ID' => $authorId,
'UF_FORUM_MESSAGE_DOC' => ['n' . $fileId] // Adding prefix 'n' to the file ID
]
]
);
if (isset($result['error'])) {
echo 'Error creating comment: ' . $result['error'];
} else {
echo 'Comment successfully created!';
}
}
// Calling the function to upload a file and create a comment
uploadFileToDisk();