# Enriching Process optimization with external data

> To enhance Process optimization in Maestro, you can extend context and visibility into the underlying business objects by connecting to external source systems—such as Salesforce or ServiceNow—that the process operates on.

To enhance Process optimization in Maestro, you can extend context and visibility into the underlying business objects by connecting to external source systems—such as Salesforce or ServiceNow—that the process operates on.

To achieve better process representation and segmentation, you need to import data from these external systems, which track business objects and their statuses, into your Process Optimization app in Process Mining. This enables more granular insights and helps identify areas for improvement. Examples include identifying product areas with high volumes of support tickets or optimizing processes based on newly discovered purchase order steps through process mining.

This page describes the steps to join external data with internal Maestro trace data to achieve this level of insight.

## Prerequisites

It is assumed that:

* Process Mining is enabled on the tenant.
* You are familiar with the internal data model, SQL, and **Process Mining** functions.
* You have Process Mining Developer role assigned. Refer to [Setting up the users](https://docs.uipath.com/process-mining/automation-cloud/latest/user-guide/setting-up-the-users) for more information.
* You have **Edit data**, **Edit dashboards**, and **View** permissions for the Process Optimization app. Refer to [Managing access for Process Mining apps](https://docs.uipath.com/process-mining/automation-cloud/latest/user-guide/managing-access-for-process-apps) for more information.
* You have built, deployed, and ran a **Maestro** process that has Integration Service activities associated with an external source system (E.g. Loan Origination process that pulls and updates records from Salesforce).

## How to enrich optimization data

To enrich your Process Optimization app with external data, connect to your source system and join the external tables with the Maestro automation data in Data transformations.

1. Go to the **Process Mining Portal**.
2. Locate the app card for the **Process Optimization** app and select **Edit in dev mode** from the context menu.
3. Select **Upload dev data** from the actions menu to upload external data.
   * **Use direct connection** option to load data from Salesforce or ServiceNow.
   * Or use the **Upload data** option to upload `CSV` or `TSV` files exported from your source system.For example, to load data from **Contact** and **Opportunity** Salesforce tables for a Loan Origination process. Refer to [Uploading data](https://docs.uipath.com/process-mining/automation-cloud/latest/user-guide/uploading-data) for more information.
   :::note
   You can view the input tables that are created (via files or direction connection) in the Input section of the Data transformations. You can select a table, to preview the data. Refer to [Input data](https://docs.uipath.com/process-mining/automation-cloud/latest/user-guide/input-data) for more information.
   :::

4. Go to **Data transformations**.
5. Locate the **models** folder in the **Transformations** section and edit the `Objects.sql` file.
6. Compare the `Object_ID` values from the `Objects.sql` file to the **ID field** from the external system table.

      :::note
      All object tables from external sources must join on this `Object_ID` field. Maestro, populates the `Object_ID` field based on the underlying object of the activity. For example, an IS activity for the **Contact** table will log the `Contact_ID` as the `Object_ID` in the `Automation_events` table). If you want to connect a **Contact** table from Salesforce, ensure the `Contact_ID` is the same format as the `Object_ID`. The following code block shows an example SQL statement that can be used in `Objects.sql` to match the `Object_ID` to the `Contact_ID` field. assignment ``` SUBSTRING(Automation_events."Object_ID",0,LEN(Automation_events."Object_ID") - 3) as "Object_ID" ```.
      :::

   :::tip
   You can use this process to join multiple sources into a single **Process optimization** app. For example, if I want to join Salesforce, ServiceNow, and Process optimization data all into the same app.
   :::

7. After confirmation of the Object_ID field from the `Automation_events` table and external `Object_ID`, join the tables on the ID fields. Add the `Trace_ID` as a field to the table to associate the external object with a Maestro run.

   The following code block shows and example join in the `contact_data.sql` file to create the `Contact` table and join it with the automation data.

      ```
      select
         contact_data.*,
         Objects."Trace_ID",
         concat('Contact',to_varchar({{ pm_utils.id() }})) as "Unique_ID"
      from {{ source('sources', 'contact_data') }} as contact_data
      left join {{ref('Objects')}} as Objects
      ON contact_data."Contact ID" = Objects."Object_ID"
      ```

      :::note
      Fields created using the `.id()` function are numeric by default. You need to cast them to `varchar`, as that is the data type of the `Event_ID` field in the `Automation_events` table.
      :::

8. Go to the **Output** section in **Data transformations**, and select **Data model** to view the data model of the **Process optimization** app.
9. Add the new external object and events tables to the data model. Make sure that the external object tables have the `Trace_ID` field populated and joined. Refer to [Data models](https://docs.uipath.com/process-mining/automation-cloud/latest/user-guide/data-models) for more information.

   The following illustration shows an example data model with the newly added `opportunity_data` and `contact_data` tables from Salesforce.

   ![Example data model with external data tables](https://dev-assets.cms.uipath.com/assets/images/maestro/maestro-example-data-model-with-external-data-tables-577257-7d82a53b.webp)

10. In the **Output** section in **Data transformations**, select **Process** to view the processes defined for the **Process optimization** app. Make sure there is a process defined with all events that will combine the events from the Maestro process and the external object events tables. Refer to [Adding and editing processes](https://docs.uipath.com/process-mining/automation-cloud/latest/user-guide/adding-and-editing-processes) for more information.
11. Select **Apply to dashboards** to make the data available for use in dashboards.
12. Edit the dashboards to visualize the external data and publish the **Process optimization** app.

    **Result:** The external data is integrated into the Process Optimization app and available for visualization in dashboards.

The following illustration shows an example process graph with external data from Salesforce.

![Example process graph with external data from Salesforce](https://dev-assets.cms.uipath.com/assets/images/maestro/maestro-example-process-graph-with-external-data-from-salesforce-577278-1aff754f.webp)
