﻿# Process events in CRM tasks

> [HTML Version](crm-events-processing.html)

The event handler helps trigger automatic launching of a set action when there are some changes in [CRM tasks](360012911980.md). You can add such event handlers in a [custom module](extentions.md).

You can track events of any CRM tasks: **Call**, **Email**, **Webinar**, **Meeting**, and [custom type](create-custom-crm-task.md). To do this, select the event handler:

- Creation. Triggered when adding a new CRM task manually or automatically using the [activities](call.md).

- Update. Triggered when modifying values of the properties from the CRM task context. For example, when the task is [reassigned](actions-with-tasks.md#reassign-task), it gets a new due date and status. When the CRM task is closed, the update event is also recorded.

- Cancellation. Triggered when the CRM task is interrupted.

- Completion. Triggered when the CRM task is closed.

````
начало внимание

````
Only [system administrators](360006871932.md) can create a module and configure event handlers in it.

````
конец внимание

## ````
Example of a handler for tracking CRM task creation

The data about the task and the associated item is sent to the handler in the form of a JSON file. It is possible to retrieve selected property values from the task context. You can then pass them to the configured business process or script with which the event handler is linked. For example, you can use the task ID, the date of its creation and completion, the user responsible for the execution, etc.

Let’s consider an example of an event handler, in which a JSON file with data about this task is processed using a script when creating a CRM task of the **Email** type. They are then passed to the business process and written to separate variables. In the process, the sales manager is given the task to read the details of the added email.

To configure the handler, follow the steps below:

1. Go to **Administration > Modules** and create a [custom module](create-extention.md).

2. In the module settings, go to the **Processes** tab and add a [business process](extention-processes.md) that will be executed each time a CRM task is created.

For our example, let’s add a simple process **Notification of a new CRM task**. Its flowchart will look as follows:

**(crm_events01.png)**

3. In the business process designer, go to the **Context** tab and add variables to which the handler will pass task data as a result of task creation. When creating each variable, select the code, type, and subtypes below: 

**(crm_events02.png)**

- **Event string representation**. A string representation of a JSON object containing data about the CRM task will be written to the variable.

- **Task name**,** Task contacts**,** Task description**,** Task start date**,** Task end date**,** Executor**. Properties to which the received data from the created task is passed using the script in the process.

4. Go to the **Flow Chart** tab. In the **Initiator** swimlane, place the [Script](360027203731.md) activity right after the start event and set the \[OBJECT\] function in it. Using the script in the function, a string representation of task data is formed from the JSON file received from the handler, and then the information is written to properties.

Example of a script to retrieve CRM task data from a JSON file

````
async function afterInit(): Promise<void> \{  
    // Form a string representation from the task constant received as a JSON file:  
    const task = JSON.parse(Context.data.event\_string\!);  
    // Now the received data can be written to the properties  
    // Retrieve the name of the CRM task:  
    Context.data.task\_name = task.\_\_name;  
    // Retrieve the description of the CRM task:  
    Context.data.task\_description = task.description;  
    // Use the loop to retrieve the list of contacts specified in the CRM task  
    Context.data.task\_contact = \[\];  
    for (const contact\_id of task.instance.contacts) \{  
        const contact = await Context.fields.task\_contact.app.search()  
            .where(  
                f => f.\_\_id.eq(contact\_id)  
            ).first();  
        Context.data.task\_contact.push(contact\!);  
    \}  
    // Retrieve the date and time of CRM task creation  
    Context.data.task\_start\_date = new Datetime(new Date(task.planStart));  
    // Retrieve the date and time of CRM task completion  
    Context.data.task\_end\_date = new Datetime(new Date(task.planEnd));  
    // Retrieve the person responsible for the task by user ID  
    const performer\_id = task.originalPerformers\[0\];  
    Context.data.task\_executor = await System.users.search().where(  
        f => f.\_\_id.eq(performer\_id)  
    ).first();  
\}
````

5. ````
Go to the flowchart and create a [static swimlane](360012263032.md#static) as the task is assigned to the sales manager recorded as an org chart item.

6. To enable the manager to view the data on the created email received from the event handler, place the [Task](360012495611.md) activity in the additional swimlane. Add the following properties to the task form: **Task name**, **Task contacts**,** Task description**,** Task start date**,** Task end date**,** **and** Executor**.

7. Save and publish the process.

8. In the module settings, go to the [Event Handling](events_processing.md) tab. Add a handler by specifying its name, the **Tasks** domain, the** CRM Task Created** event, the **Email** task type, and **Run Script** handler type.

**(crm_events03.png)**

9. On the event handler settings page that opens, click the **Scripts** tab. In the action function, specify the script that will convert the JSON file into a string representation and launch the previously configured **Notification of a new CRM task** process.

````
async function action(): Promise<void> \{  
    // Retrieve a string representation of the data on the task whose creation has been triggered by the handler and save it to the task\_json\_string constant  
    const task\_json\_string = JSON.stringify(Context.data.task);  
    // Start the previously created business process by passing task\_json\_string to the Event string representation variable from the process context:  
    await Namespace.processes.crm\_task\_notification.run(\{  
        event\_string: task\_json\_string,  
    \})  
\}

10. ````
On the top panel of the page, click **Save** and then **Publish** to activate the event handler.

11. Go back to the main page of the module and enable it.

Now with the module enabled, when a user creates a CRM task of the **Email **type, an event handler will be triggered in the system. The script will launch the configured business process. The sales manager will receive the task, where they will be able to review all the task data.