How to Add a Comment to the Timeline of a Smart Process

Scope: crm

Who can execute the method: users with permission to modify the CRM entity

The key parameter for adding a comment to a CRM entity is the object type identifier. This identifier indicates which type of object the comment will be added to: a deal, a lead, or a specific smart process. The identifier is used in the parameters OWNER_TYPE, OWNER_TYPE_ID, ENTITY_TYPE, and ENTITY_TYPE_ID of the method groups crm.item.*, crm.timeline.*, crm.activity.*.

There are two types of object identifiers in CRM:

  • Predefined — these are identifiers for leads, deals, companies, contacts, invoices, and estimates. The identifiers for predefined objects can be found in the documentation.
  • Dynamic — these are identifiers for smart processes. The smart process identifier is generated at the time of creation and does not depend on the name of the smart process.

You can obtain the identifier of a smart process using two methods:

  • crm.enum.ownertype — a method without parameters that returns an enumeration of CRM object types, both predefined and dynamic.
  • crm.type.list — a method with a filter that returns only dynamic CRM objects.

To create a comment in a smart process entity, we will sequentially execute two methods:

  1. crm.type.list — retrieve the smart process by filter.
  2. crm.timeline.comment.add — create the comment.

1. Retrieve the Smart Process Type Identifier

To obtain the type identifier, we use the method crm.type.list with a filter:

  • title — specify the name of the smart process.

How to Use Examples in Documentation

BX24.callMethod(
            'crm.type.list',
            {
                filter: {
                    "title": "Equipment Purchase"
                }
            }
        );
        
require_once('crest.php');
        
        $result = CRest::call(
            'crm.type.list',
            [
                'filter' => [
                    'title' => 'Equipment Purchase'
                ]
            ]
        );
        

As a result, we obtain two ID values:

  • id: 7 — the ordinal number of the smart process in Bitrix.
  • entityTypeId: 177 — the identifier of the smart process type. This parameter is necessary for the next request.
{
            "result": {
                "types": [
                    {
                        "id": 7,
                        "title": "Equipment Purchase",
                        "code": "",
                        "createdBy": 1,
                        "entityTypeId": 177,
                        "customSectionId": null,
                        "isCategoriesEnabled": "Y",
                        "isStagesEnabled": "Y",
                        "isBeginCloseDatesEnabled": "Y",
                        "isClientEnabled": "Y",
                        "isUseInUserfieldEnabled": "Y",
                        "isLinkWithProductsEnabled": "Y",
                        "isMycompanyEnabled": "Y",
                        "isDocumentsEnabled": "Y",
                        "isSourceEnabled": "Y",
                        "isObserversEnabled": "Y",
                        "isRecyclebinEnabled": "Y",
                        "isAutomationEnabled": "Y",
                        "isBizProcEnabled": "Y",
                        "isSetOpenPermissions": "Y",
                        "isPaymentsEnabled": "N",
                        "isCountersEnabled": "N",
                        "createdTime": "2021-11-26T10:52:17+03:00",
                        "updatedTime": "2024-11-12T15:32:39+03:00",
                        "updatedBy": 1
                    }
                ]
            }
        }
        

2. Add a Comment to the Smart Process Entity

To add a comment, we use the method crm.timeline.comment.add with the following parameters:

  • ENTITY_ID — ID of the entity. To obtain the ID value, use the method crm.item.list, where entityTypeId filter equals the entityTypeId value from crm.type.list.
  • ENTITY_TYPE — specify DYNAMIC_177. The value consists of the entityTypeId from the result of the previous method and the prefix for dynamic objects DYNAMIC_.
  • COMMENT — the text value of the comment.
BX24.callMethod(
            "crm.timeline.comment.add",
            {
                fields:
                {
                    "ENTITY_ID": 19,
                    "ENTITY_TYPE": "DYNAMIC_177",
                    "COMMENT": "Confirm the purchase via e-mail!",
                }
            }
        );
        
require_once('crest.php');
        
        $result = CRest::call(
            'crm.timeline.comment.add',
            [
                'fields' => [
                    'ENTITY_ID' => 19,
                    'ENTITY_TYPE' => 'DYNAMIC_177',
                    'COMMENT' => 'Confirm the purchase via e-mail!',
                ]
            ]
        );
        

We added a comment to the timeline of the smart process entity and received the timeline record ID 55771 in response. This record ID can be used in the methods for updating and deleting the comment.

{
            "result": 55771
        }
        

Code Example

// Function to find the smart process identifier
        function findSPA() {
            // Name of the smart process to obtain entityTypeId
            var SPAtitle = 'your_smart_process_name';
        
            // Call the crm.type.list method to get entityTypeId
            BX24.callMethod(
                'crm.type.list',
                {
                    filter: {
                        title: SPAtitle
                    }
                },
                function(result) {
                    if (result.error()) {
                        console.error('Error finding smart process:', result.error());
                    } else {
                        var types = result.data().types;
                        if (Array.isArray(types) && types.length > 0) {
                            var SPAId = types[0].entityTypeId; // Assuming the desired object is the first in the array
                            console.log('Smart process found', SPAId);
                            createComment(SPAId);
                        } else {
                            console.error('Smart process not found or data is empty');
                        }
                    }
                }
            );
        }
        
        // Function to create a comment in the smart process entity
        function createComment(SPAId) {
            // ID of the entity to which the comment will be added
            var elementId = 'your_element_ID';
            // Text of the comment
            var commentText = 'your_comment';
        
            // Call the crm.timeline.comment.add method to add the comment
            BX24.callMethod(
                "crm.timeline.comment.add",
                {
                    fields: {
                        ENTITY_ID: elementId,
                        ENTITY_TYPE: 'DYNAMIC_' + SPAId,
                        COMMENT: commentText
                    }
                },
                function(result) {
                    if (result.error()) {
                        console.error('Error creating comment:', result.error());
                    } else {
                        console.log('Comment added', result.data());
                    }
                }
            );
        }
        
        // Call the function to find the smart process and add a comment
        findSPA();
        
require_once('crest.php');
        
        // Function to find the smart process identifier
        function findSPA() {
            // Name of the smart process to obtain entityTypeId
            $SPAtitle = 'your_smart_process_name';
        
            // Call the crm.type.list method to get entityTypeId
            $result = CRest::call(
                'crm.type.list',
                [
                    'filter' => [
                        'title' => $SPAtitle
                    ]
                ]
            );
        
            if (isset($result['error'])) {
                echo 'Error finding smart process: ' . $result['error'];
            } else {
                $types = $result['result']['types'];
                if (is_array($types) && count($types) > 0) {
                    $SPAId = $types[0]['entityTypeId']; // Assuming the desired object is the first in the array
                    echo 'Smart process found: ' . $SPAId;
                    createComment($SPAId);
                } else {
                    echo 'Smart process not found or data is empty';
                }
            }
        }
        
        // Function to create a comment in the smart process entity
        function createComment($SPAId) {
            // ID of the entity to which the comment will be added
            $elementId = 'your_element_ID';
            // Text of the comment
            $commentText = 'your_comment';
        
            // Call the crm.timeline.comment.add method to add the comment
            $result = CRest::call(
                'crm.timeline.comment.add',
                [
                    'fields' => [
                        'ENTITY_ID' => $elementId,
                        'ENTITY_TYPE' => 'DYNAMIC_' . $SPAId,
                        'COMMENT' => $commentText
                    ]
                ]
            );
        
            if (isset($result['error'])) {
                echo 'Error creating comment: ' . $result['error'];
            } else {
                echo 'Comment added';
            }
        }
        
        // Call the function to find the smart process and add a comment
        findSPA();