1. Problem
You need to map values from the source to the destination message depending on whether a logical condition evaluates to true or false.
2. Solution
Define the logical
condition that the mapping actions should be based on by dragging
logical functoids from the toolbox onto the map surface. Any functoid or
combination of functoids that returns a Boolean value can establish the
logical condition, but these functoids can return only true or false.
In this example, the logical condition will check whether the amount of a sale is greater that 1,000, as shown in Figure 1.
Only large sales should appear under the <BigSales> element in the destination message, so the logical condition will check if the Amount
in the source message is greater than 1,000. The following steps define
the logical operation that determines mapping actions, as shown in Figure 2.
Drag a Greater Than functoid from the Logical Functoids tab of the toolbox onto the map surface.
Select the Amount
field of the input message. Drag the cursor over the Greater Than
functoid while pressing the mouse button to establish the first
comparison value.
Right-click the functoid on the map surface, and select Properties.
In the Properties window, click the Input Parameters field, and select the ellipsis that appears.
Define the constant value in the Configure Functoid Inputs dialog box.
Next, define the mapping actions that depend on the evaluation of the logical condition. In this example, the contents of the <Sales>
elements should map to different parts of the destination message, so
the map must use the Boolean outcome of the logical comparison as input
to a Value Mapping functoid.
Drag a Value Mapping functoid from the Advanced Functoids tab of the toolbox to the right and below the Greater Than functoid.
Create a link from the Greater Than functoid to the Value Mapping functoid.
Create a link from the Amount field of the input message to the Value Mapping functoid to determine the value mapped when the logical condition is true.
Create a link from the Value Mapping functoid to the SaleAmount field appearing under the BigSales record to define where the large value will occur in the destination message.
The Value Mapping functoid defines the action the map should take when the logical condition evaluates to true.
Both the amount of the big sale and the name of the sales
representative must appear in the destination message in this example,
so the map must use two Value Mapping functoids, as shown in Figure 3.
BizTalk must also know that it should create only the <RepSales> element in the destination message when the amount is greater that 1,000. Establishing a link from the logical operator to the RepSales element will ensure that BizTalk creates this parent to the actual values mapped.
When the logical condition evaluates to false, BizTalk should map the sale to the <SmallSales>
part of the destination message. The map can check this directly by
placing a Not Equal functoid on the mapping surface and comparing to the
result of the logical comparison established earlier with a constant
value equal to true, as shown in Figure 4.
Finally, define the mapping
actions that BizTalk should take when the logical comparison is not
true. In this example, the Value Mapping functoids simply need to map
under the <SmallSales> element in the destination message.
Drag a Value Mapping functoid onto the map surface.
Create a link from the Not Equal functoid that represents the else case to the Value Mapping functoid.
Create a link from the Amount field of the source message to the Value Mapping functoid.
Create a link from the Value Mapping functoid to the SaleAmount field appearing under the SmallSales record to define where the small value will occur in the destination message, as shown in Figure 5.
3. How It Works
Working with the BizTalk
Mapper can be an unfamiliar process, even to experienced programmers.
Business rules defined in source code can be procedural and explicit,
but the graphical depiction of a BizTalk map can abstract the runtime
behavior.
The link to the logical
condition defines the information on which the map will perform the
logical operation. BizTalk will evaluate the logical condition for each
occurrence of the information in the source message, and remember the
context of the logical condition when performing the output actions
dependent on it. This means that the Value Mapping functoid will
automatically know the right Amount and RepName values to map when the map executes.
The order of the Value Mapping functoid inputs is critical. The first input parameter must be either true or false. The second input parameter specifies the value that the functoid will return when the first parameter is true.
While the order is established based on the order in which the inputs
are connected to the Value Mapping functoid, sometimes the order can get
mixed up when modifying the functoid links. There is no indication of
the input parameter order on the mapping surface, but fortunately, it is
easy to check. Open the Configure Functoid Inputs window, and the
Boolean value should appear first in the list. Modify the order here by
selecting one of the input parameters and clicking the up and down
arrows.
NOTE
In many cases,
If-Then-Else logic using functoids like this can becoming complex and
frustrating to implement (and maintain!). The best approach frequently
is to use an Advanced Scripting functoid with either XSLT or C# to
implement the logic.
This example establishes a
logical condition and defines mutually exclusive mapping actions that
are directly determined from the evaluation of the logical conditions.
Alternatively, a map can define separate logical conditions
independently. With this alternative approach, the developer must
carefully define the logical conditions to ensure that every input
message will map to some outcome, and never both. This becomes an
increasingly important consideration as the logical comparison becomes
more and more complex.
When there are more than two
mutually exclusive outcomes, the Not Equal functoid in this example can
be combined with additional logical comparisons by linking its result to
the Logical AND functoid. The result of the Logical AND functoid also
evaluates to true or false, and the developer can define actions to take based on this result, as with any other logical operator.