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 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
Copy

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
Copy

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
Copy

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:

1
exports.step = function (input) {
2
// The mappings that will eventually be added by your end user:
3
var mapping = input.keyDataMappings;
4
// The new object we will use to create an entity to insert:
5
var newEntity = {};
6
for (var field in mapping) {
7
newEntity[mapping[field]] = input.webhookFields[field];
8
}
9
return newEntity;
10
};

3 - Create the 'Map values' script
Copy

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:

1
exports.step = function (input) {
2
// The mappings that will eventually be added by your end user:
3
var mapping = input.valueDataMappings;
4
// The new object we will use to create an entity to insert:
5
var newEntity = {};
6
for (var field in input.mappedFields) {
7
var currentValue = input.mappedFields[field];
8
newEntity[field] = mapping[currentValue] || currentValue;
9
}
10
return newEntity;
11
};

4 - Send the mapped result for processing
Copy

The mapped result can then be sent for processing using $.steps.script-2.result

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

keyDataMappings
Copy

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

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

service-1
Copy

service-2
Copy

Default mapping
Copy

valueDataMappings
Copy

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
Copy

Service 2
Copy

Default mapping
Copy