CRM > CRM tasks / Event handling in CRM tasks

Event handling in CRM tasks

For CRM tasks, you can create event handlers that use scripts or business processes to run certain actions in the system after the events have occurred.

For example, when you create a Webinar activity, the event handler will run a process where the executor will be tasked with creating and coordinating the webinar plan with the manager.

The event handler is configured as part of creating a custom module. You can pass the CRM tasks context, as well as the ID task and the user responsible for its execution to the handler.

начало внимание

Only users in the Administrators group can create event handlers.

конец внимание 

Create an event handler

We will look at the creation of an event handler using an example.

Let’s assume that when we create a Webinar activity, the performer is assigned the task of creating and uploading an event plan to the system for subsequent approval by the manager. After the webinar, the employee needs to upload a webinar report.

To do this:

  1. Go to Administration > Modules and create a custom module.
  2. In the module settings, add a business process or script that will be executed when the event is reached.

For example, a business process created on the Processes tab might look like this:

crm_events01

  1. Click the Event Handling tab.
  2. Click +Handler and set the parameters:

crm_events02

  • Name*. Enter a name for the event handler, for example, New webinar.
  • Event domain. Specify the domain in which events will be tracked by the handler. To handle events in CRM tasks, select Tasks.
  • Event. Specify the event that will run the handler: Task cancellation, Task completion, Task creation, Task update. In this case, select Task creation.
  • Task type filter. Select the type of CRM tasks to be handled: All CRM tasks, Call, Email, Webinar, Meeting. For our example, select Webinar.
  • Handler type*. Select the action that will be performed when the event is executed: Run script, Run process, Send message to service (available only for ELMA365 On-Premises). In our case, we select Run process.
  1. Click Create.
  2. On the opened event handler settings page, specify the process or script created inside the user module that will be launched at the selected event. In our case, specify the business process created in step 2.

crm_events03

  1. On the top panel of the page, click Save and then Publish to activate the event handler.
  2. Go back to the home page of the module and enable it.

After that, when creating an activity of Webinar type, the event handler will be run in the system, which will start the specified business process. During this process, the employee will be tasked to generate an event plan and then coordinate it with the manager

Get data from a CRM task

When creating an event handler, you can use data from the CRM element associated with the activity. For example, you can pass to a business process or script the context of the activity, as well as the ID of the task and the user responsible for its execution.

Below is an example of a function that retrieves data from a JSON file about a task and the element on which it is assigned. The retrieved data can be processed within a business process that is run by an event handler.

In this example, the event handler runs the process after a CRM task of type Email is created.

Place a Script on the business process modeling field right after the start event. Create a new function in the activity with the following content:

async function afterInit(): Promise<void
{
    /*Context.data.element is a variable from the business process context with the Arbitrary app type, into which the element associated with the CRM task should be written as part of the handler configuration*/
    const element = await Context.data.element!.fetch();
 
    /*element.code is the element code by which you can determine the type of the associated element (contract, lead, etc.) in order to run the process only for an element of a certain type*/
    / To run the process only for a certain type (for example, only for contracts), it is recommended to place a gateway that checks the value of the variable containing the element code after the afterInit script run activity*/
    Context.data.element_code = element.code;
 
    /*Context.data.event is a variable from the business process context with the Arbitrary app type, into which a JSON file with CRM task data should be written as part of the handler configuration*/
    /*event.originalPerformers[] is the id of the user assigned to be responsible for the CRM task*/
    const performer: string = Context.data.event.originalPerformers[0];
 
    /*Find the responsible user by ID and write it to the variable Context.data.performer from the business process context with type Users*/
    Context.data.performer = await System.users.search().where(f => f.__id.eq(performer)).first();
}

Found a typo? Highlight the text, press ctrl + enter and notify us