# Many-to-one mapping

It is possible to implement many-to-one mapping for your Embedded solutions.

It is possible to implement many-to-one mapping for your Embedded solutions.
For example, you may wish 'Country' in one service to be mapped to 'Region' in another:

| Service one key | Service one values         | Service two key | Service two value |
| --------------- | -------------------------- | --------------- | ----------------- |
| Country         | US, Canada                 | Region          | North America     |
|                 | UK, Germany, France, Italy |                 | EU                |
|                 | India, Pakistan, Sri Lanka |                 | South Asia        |

Or you may wish 'Status' in one service to be mapped to 'Availability' in another:

| Service one key |         Service one values         | Service two key |   Service two value  |
| :-------------- | :--------------------------------: | :-------------: | :------------------: |
| Status          |        available, part-time        |   Availability  |  available for work  |
|                 | unavailable, full-time, sick-leave |                 | unavailable for work |

Note that in both these cases:

* **Key** mapping is one-to-one
* **Value** mapping is many-to-one

## Many-to-one example

The below example puts the second table above into practice.
It imagines a scenario whereby a solution is triggered by a webhook coming from a service which alerts requests for work availability - including the name and status of the individual in question:
![many-to-one-full-workflow](https://tray.ai/documentation/images/platform/embedded/advanced-features/data-mapping/many-to-one-mapping/7e51c4ef-9c6e81aa_many-to-one-full-workflow-2.png)
The main steps here are:

1. The webhook is received
2. The JSON data is parsed with an Object Helpers step (**optional** - only if the formatting of the json needs it)
3. A script is set up to map the **Keys**
4. A script is set up to map the **Values**
5. The mapped information is then sent to be processed into the second service (here it is done by sending to a callable workflow set up for sub-processing)
6. Trigger Event Reply is used to reply to the original service and indicate that the request has been successfully received (**optional**)

## 1 - Receive and parse webhook data

First of all we have set up an Object Helpers step to Parse the JSON coming from the webhook:
![parse-json](https://tray.ai/documentation/images/platform/embedded/advanced-features/data-mapping/many-to-one-mapping/7e51c4ef-af46860c_webhook-parse-json.png)

## 2 - Create the 'Map keys' script

Then we configure the **Map keys** script:
![map-keys-script](https://tray.ai/documentation/images/platform/embedded/advanced-features/data-mapping/many-to-one-mapping/7e51c4ef-01362059_map-keys-script.png)
Note that this has two inputs:

* **keyDataMappings** which uses the config value `$.config.keyDataMapppings` (this is the config that is made available for the End User to set the mappings in the Config Wizard)
* **webHookFields** which pulls the parsed json fields from the webhook using `$.steps.object-helpers-1`
  From the debug output above you will see that the purpose of this is to map the **keys** of the fields from **name** and **status** to **Name** and **Availability**.
  The script itself is a standard mapping script:

```javascript
exports.step = function (input) {
 // The mappings that will eventually be added by your end user:
 var mapping = input.keyDataMappings;
 // The new object we will use to create an entity to insert:
 var newEntity = {};
 for (var field in mapping) {
  newEntity[mapping[field]] = input.webhookFields[field];
 }
 return newEntity;
};
```

## 3 - Create the 'Map values' script

Then we configure the **Map values** script:
![map-values-script](https://tray.ai/documentation/images/platform/embedded/advanced-features/data-mapping/many-to-one-mapping/7e51c4ef-4b2ff364_map-values-script.png)
Note that this has two inputs:

* **valueDataMappings** which uses the config value `$.config.valueDataMapppings` (this is the config that is made available for the End User to set the mappings in the Config Wizard)
* **mappedFields** which pulls the result from the first script using `$.steps.script-1.result`
  From the debug output above you will see that the purpose of this is to map the **values** of the fields. In this case it maps **part-time** to **available for work**.
  It is key to note here that the **values mapping script is slightly different**:

```js
exports.step = function (input) {
 // The mappings that will eventually be added by your end user:
 var mapping = input.valueDataMappings;
 // The new object we will use to create an entity to insert:
 var newEntity = {};
 for (var field in input.mappedFields) {
  var currentValue = input.mappedFields[field];
  newEntity[field] = mapping[currentValue] || currentValue;
 }
 return newEntity;
};
```

## 4 - Send the mapped result for processing

The mapped result can then be sent for processing using `$.steps.script-2.result`
![call-workflow-for-processing](https://tray.ai/documentation/images/platform/embedded/advanced-features/data-mapping/many-to-one-mapping/7e51c4ef-2d4d424c_m21-call-workflow-for-processing.png)

## 5 - Set up the available mappings in the Solution Editor

### keyDataMappings

In the Solution Editor we can set the mappings for **keyDataMappings**:

![solution-editor-keydatamappings](https://tray.ai/documentation/images/platform/embedded/advanced-features/data-mapping/many-to-one-mapping/7e51c4ef-2b6777a9_solution-editor-keydatamappings.png)

Note that this is **not set** as 'Many-to-one':

#### Key Mapping: service-1

![key-mapping-service-1](https://tray.ai/documentation/images/platform/embedded/advanced-features/data-mapping/many-to-one-mapping/7e51c4ef-3b1c96aa_key-mapping-service-1.png)

#### Key Mapping: service-2

![key-mapping-service-2](https://tray.ai/documentation/images/platform/embedded/advanced-features/data-mapping/many-to-one-mapping/7e51c4ef-63d5a0be_key-mapping-service-2.png)

#### Key Mapping: Default

![key-mapping-default](https://tray.ai/documentation/images/platform/embedded/advanced-features/data-mapping/many-to-one-mapping/7e51c4ef-62545ab6_key-mapping-default.png)

### valueDataMappings

And then we can set it up for **valueDataMappings**:

![solution-editor-valuedatamappings](https://tray.ai/documentation/images/platform/embedded/advanced-features/data-mapping/many-to-one-mapping/7e51c4ef-694a4fad_solution-editor-valuedatamappings.png)

Click through the tabs to see the setup for each service and the default setting.

Note that this **is set as** 'Many-to-one':

#### Value Mapping: service-1

![value-mapping-service-1](https://tray.ai/documentation/images/platform/embedded/advanced-features/data-mapping/many-to-one-mapping/7e51c4ef-821663f6_value-mapping-service-1.png)

#### Value Mapping: service-2

![value-mapping-service-2](https://tray.ai/documentation/images/platform/embedded/advanced-features/data-mapping/many-to-one-mapping/7e51c4ef-0d967c30_value-mapping-service-2.png)

#### Value Mapping: Default

![value-mapping-default](https://tray.ai/documentation/images/platform/embedded/advanced-features/data-mapping/many-to-one-mapping/7e51c4ef-289ee029_value-mapping-default.png)
