﻿# Process events in duplicates

> [HTML Version](duplicates-events-processing.html)

When the search for similar items is enabled in the app, you can create an event handler that will track the merging of duplicates and trigger the necessary actions through a script or business process.

Event handlers are created within [custom modules](extentions.md). You can pass to the handler a link to the main item and the record marked as a duplicate, as well as properties from their context.

````
начало внимание

````
Only users who are members of the [Administrators](360006871932.md#administrators) group can create a module and configure the handler in it.

````
конец внимание

## ````
Example of the event handler tracking the duplicate merge

Let’s consider an example of creating an event handler for **CRM** workspace apps that have duplicate search enabled.

For example, a user finds two items with the same data and combines them into one record. The added event handler captures this action and starts the process. In the course of the process, the data on the main and merged item is passed using scripts. Then the responsible employee and their manager are sent the task of reviewing the list of merged duplicates.

To configure the handler, perform the following steps:

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 after the duplicate records are merged.

For our example, let’s add the **Duplicate merge notification **process and configure its flowchart as follows:

**(duplicates-events-processing-1.png)**

3. In the business process designer, go to the **Context** tab and add variables to which the handler will pass links to the main item and merged records as a result of merging duplicates:

- **Merged Item** is a property of the **Arbitrary App (One)** type.

- **Duplicates** are properties of the **Arbitrary App (Many)** type.

4. On the **Flow Chart** tab, place two [Script](360027203731.md) activities and set the functions that will be used to define the main item and its duplicate records.



1. Example of a script for obtaining data on duplicates

````
async function getDoubles(): Promise<void> \{  
  // \`Context.data.doubles\` is the Duplicates variable from the context of the Duplicate merge notification process  
  // When an event handler is created, it is configured to store all the items that are merged during the search for duplicates into this variable.  
  // To find out which app the duplicates belong to, and to determine which properties can be accessed within the script, we need to get the first item in the \`Context.data.doubles\` list and read its \`code\` property:  
  let element = await Context.data.doubles\!\[0\].fetch(); // Retrieve the first item of the \`Context.data.doubles\` list.  
  let doubles\_app\_code = element.code; // Read the \`code\` property.  
  // Checking the value of the \`code\` property allows you to generate a custom item processing script for each app in which the duplicate search is activated:  
  switch(doubles\_app\_code) \{  
      case "\_opportunities":  
          // Create the Duplicate Leads variable in the business process context (type: App > Leads with the Many option, access code: \`Context.data.opportunities\_doubles\`). This variable stores links to duplicate records within the Leads app:  
          Context.data.opportunities\_doubles = Context.data.doubles as BaseApplicationItem<Application\$\_clients\$\_opportunities\$Data, Application\$\_clients\$\_opportunities\$Params>\[\];  
          // It is now possible to handle duplicates in the Leads app:  
          for (let i = 0; i < Context.data.opportunities\_doubles\!.length; i++) \{  
              let opportunity\_double = await Context.data.opportunities\_doubles\[i\].fetch(); // Get the item in the loop  
              let opportunity\_name = opportunity\_double.data.\_\_name; // Retrieve the name of the item. Values of other app properties can be accessed in the same way.  
          \}  
          break;  
      case "\_leads":  
          // When accessing this app, processing of deal duplicates can be implemented.  
          break;  
      case "\_companies":  
          // When accessing this app, processing of company duplicates can be implemented.  
          break;  
      case "\_contacts":  
          // When accessing this app, processing of contact duplicates can be implemented.  
          break;  
  \}  
\}
````

````
2. Example of a script for obtaining data on the main (merged) item

````
async function getMainElement(): Promise<void> \{  
    // Processing of the main (merged) item is similar to the processing of duplicate records. The main difference is that there are always several duplicates, while there is only one main item.  
    let element = await Context.data.main\_element\!.fetch(); // Retrieve the first item of the \`Context.data.doubles\` list.  
    let doubles\_app\_code = element.code; // Read the \`code\` property.  
    // By checking the value of the \`code\` property, you can generate a custom item processing script for each app in which the duplicate search is activated  
    switch(doubles\_app\_code) \{  
        case "\_opportunities":  
            // Create the Main Lead variable in the business process context (type: App > Leads with the One option, access code: \`Context.data.opportunity\_main\_element\`). This variable stores a link to main lead:  
            Context.data.main\_element\_opportunity = Context.data.main\_element\! as BaseApplicationItem<Application\$\_clients\$\_opportunities\$Data, Application\$\_clients\$\_opportunities\$Params>;  
            // The main item of the Leads app can now be processed:  
            let main\_element\_opportunity = await Context.data.main\_element\_opportunity.fetch(); // Obtain the item  
            let main\_opportunity\_name = main\_element\_opportunity.data.\_\_name; // Retrieve the item name. Values of other app properties can be accessed in the same way  
            break;  
        case "\_leads":  
            // When accessing this app, processing of deal duplicates can be implemented.  
            break;  
        case "\_companies":  
            // When accessing this app, processing of company duplicates can be implemented.  
            break;  
        case "\_contacts":  
            // When accessing this app, processing of contact duplicates can be implemented.  
            break;  
    \}  
\}
````

````


5. Since the participants of the process will be determined while it’s executed, create two [dynamic swimlanes](360012263032.md#dynamic) on the process diagram:

- For employees responsible for the app item.

- For sales reps.

6. To allow employees to view the list of duplicates found by the event handler, place [Task](360012495611.md) activities in the swimlanes and add the **Merged Item** and **Duplicates** variables to the task forms.

7. Save and publish the process.

8. In the module settings, go to the [Event Handling](events_processing.md) tab. Add a handler, specifying the name, the event domain: **Duplicates**, the event: **Merge duplicates**, and the handler type: **Start process**.

**(duplicates-events-processing-2.jpg)**

9. On the event handler settings page that opens, specify the process created earlier inside the custom module.

10. Click the **Input Variables** tab and assign data sources for the variables from the process context in the **Item Field** column. To do this, map them to the event properties.

The **Merge Duplicates** event has a set of system fields (event ID, deadline, name of the object, author, etc.), as well as individual properties: link to the main item and duplicates. For our example, we need to map the **Duplicates** and **Merged Item** variables.

**(duplicates-events-processing-3.png)**

Please note that the **Input Variables** tab displays properties that are present in the process context at the time the process is bound to the event handler. If the process context has been changed, the new variables will not appear in the handler settings. You have to create and configure the event handler again, binding the same business process to it.

11. On the top panel of the settings page, click **Save** and **Publish** to activate the event handler.

12. Go back to the main module page and enable the module.

Now, when the user merges multiple duplicate items, an event handler will be triggered in the system, which will start the configured business process. The responsible employees and the manager will review similar records in the task form.