Ravindar, Integrations with Mulesoft & BizTalk

Just another Integration blog

Enrich payload with new fields using Message Enricher

Observation Notes:

We had a requirement of augmenting the current payload with few extra fields which we need to get from external service and then augment it to current payload.

We’ve used Message Enricher.
For example, we have a incoming Json payload like below


{
	"EmployeeID": "92"
}

Now you want to enrich your payload with extra fields like below. The two extra fields will have to get from external service and augment it to the original payload

{
	"EmployeeID": "92",
	"Name": "Test Name",
	"Location": "Test Location"
}

The flow goes like this

Message Enricher Smaple flow

The message enricher receives the copy of original message payload with just EmployeeID. The message enricher has two components here
1. Http call to exernal service to get the extra details of the employee
2. A transform shape (this is just to convert the response to java object). The transform shape code is like below. It just assigns the payload AS-IS, just changes to java object

%dw 1.0
%output application/java
---
payload

Now in the message enricher properties, set it like below

Message Enricher properties

Since we are enriching/augumenting only specific fields, we should use the “Enrich elements” section. And in that, the “Source” section indicates the payload inside the Enricher component (which is the response we got from the http call). And the “Target” section on the right side indicates the payload of the main flow.
so here, it will pick the “location” and “name” fields from the response of the http call, and adds those fields as “EmployeeLocation” and “EmployeeName” in the payload of the main flow.

so the result of the main flow final payload will be as desired like below

{
	"EmployeeID": "92",
	"Location": "Test Location",
	"EmployeeName": "Test Name"
}

Other regular usage of Enricher components are like

Another CASE I:
We want to retain the main payload As-IS but would like to store the response of the http call into a flow variable. Then set the properties of the enricher like below

Message Enricher properties_flowvar

So here, notice that in the source field we did not mention anything, in this case, after the Enricher component is executed, the output of the enricher component will be stored in the flow variable “postEnrich” as named above. However the main flow payload will still remain the same as the old original payload as below


{
	"EmployeeID": "92"
}

Another CASE 2:
well…we can play with it in many ways….

September 26, 2019 - Posted by | Mule 3, Mulesoft

No comments yet.

Leave a comment