Wednesday, August 19, 2009

BizTalk: Create dynamically nodes with mapper and looping

Problem:
Sometimes, we receive data as input, but need to split this data in the output.
Ex. As Input we receive 2 dates, begin-date and end-date.
The output should be for every date between the given dates 1 node

Input Data
Begin Date: 19-8-2009
End Date: 25-8-2009

Output Data
19-8-2009
20-8-2009
21-8-2009
22-8-2009
23-8-2009
24-8-2009
25-8-2009

Our customers always used an external web service to do this or a C# class (placed in the GAC). I was wondering if it would be possible to do the same 100% out of the ox, so when we deploy we don’t need to care of the extra class in the GAC or web services and security.

Solution in BizTalk

1. Create Input Schema




2. Create Output Schema



3. Create Mapping



Source Schema: InputSchema

Destination Schema OutputSchema



Script 1: This script is an inline C#, calculating the different days between the begin date and the end date. The script returns an integer.

public int SubtractDates(System.String param1, System.String param2)
{
System.TimeSpan diffresult = System.Convert.ToDateTime(param2) - System.Convert.ToDateTime(param1);
return diffresult.Days;
}


Script 2: Select Inline XSLT Call Template. The XSLT template is recursive. The first part is handling the receiving parameters. The second part is the creation of a node in the output schema (). The third part, we call the same XSLT template, with the updated variables

Pay attention on the last parameter startDate, we call a inline CSharp Function (see further)

The reason why we need to call an inline CSharp function is the fact that BizTalk handles only XSLT 1.0, so we can’t use some great date time functions.
(the lines starting with ------ must be deleted, it is just to explain the different parts of the code)

------ Parameters --------






------ Creation of a node --------








------ recersive Part --------

















Script 3: This script functoid has no input or output parameters, but we create Inline C# function, AddDaysToStartDate. The function will just add a number of days to the given start date. (Parameters coming from script 2)

public System.DateTime AddDaysToStartDate(string param1, string param2)
{
DateTime _date = System.Convert.ToDateTime(param1);
return _date.AddDays(System.Convert.ToDouble(param2));
}

Test the mapping and you will see that the output schema contains an interval node for everyday between begin and end date.