Tray Embedded / Advanced Topics / Data Mapping (Embedded) / Many-to-one Mapping

Many-to-one Mapping

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 keyService one valuesService two keyService two value
CountryUS, CanadaRegionNorth America
UK, Germany, France, ItalyEU
India, Pakistan, Sri LankaSouth Asia

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

Service one keyService one valuesService two keyService two value
Statusavailable, part-timeAvailabilityavailable for work
unavailable, full-time, sick-leaveunavailable 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:

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:

2 - Create the 'Map keys' script

Then we configure the Map keys script:

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:

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:

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:

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

5 - Set up the available mappings in the Solution Editor

keyDataMappings

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

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

service-1

service-2

Default mapping

valueDataMappings

And then we can set it up for valueDataMappings

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

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

service-1

Service 2

default-mapping">Default mapping