Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / biztalk

Biztalk Datetime Functoids

5.00/5 (2 votes)
7 Aug 2012CPOL8 min read 39.5K   391  
Biztalk Datetime Functoids

Content

Introduction

This article assumes you have a basic understanding of mapping. Also, this will mostly useful for people who are practicing on datetime functoids. I recommend you to read the entire article before getting into the attached files.

This article will cover the below topics:

  • Existing Datetime/ related functoids.  
  • Using C# script functoid for Datetime validation Extension
  • Consuming C# script DLL in script functoid/mapping app.

Also we will be covering the below general scenarios,

  1. To check if the specified input string value is valid a datetime or not and pass/display resultant value in output file if it is valid.
  2. Convert the local date time to UTC/GMT datetime.
                 E.g.
                 Input value : 9/1/2008 1:00:00 AM
                 Output value : 8/31/2008 7:30:00 PM (converted into local)
                 Note: Here the local time(input value) is in Indian timezome which is (GMT+5:30)
  3. Pass the UTC Datetime/ Any time zone Datetime and fetch only time.
                 E.g.
                 Input value : 2012-07-23T23:10:00Z
                 Output value : 04:40 // this is as per user local time zone
  4. Pass the UTC Datetime/Any time zone Datetime and fetch only Month out of it (local time zone format)
                 E.g.
                 Input value : 2012-07-23T23:10:00Z
                 Output value : 7
  5. Pass the UTC Datetime/any time zone Datetime and fetch only Year out of it (local time zone format)
                 E.g.
                 Input value : 2012-07-23T23:10:00Z
                 Output value : 2012
  6. Pass the UTC Datetime/ any time zone Datetime and fetch Month and Date (specific format) out of it.
                 E.g.
                 Input value : 2012-07-23T23:10:00Z
                 Output value : July 24
  7. Find the difference between the two datetimes.
                 E.g.
                 Input value : 09/11/1982T12:00:00-05:30
                 Output value : 29 years, 10 months, 3 weeks, 5 days.
                 Note: internally it is comparing with today system datetime ,which is in 07/26/2012T14:00:00Z
  8. Compare two datetimes.
                 E.g.
                 Input value : 09/11/1982T12:00:00-05:30
                 Result value : Smaller
  9. Pass the UTC Datetime and convert it into local datetime.
                 Eg:
                 Input value : 2012-07-23T23:10:00Z
                 Result value : 7/24/2012 10:10:00 AM
                 Note: The result is in an Indian timezone which is (GMT+5:30). The other thing I have not concentrated on is the format of date, which we have seen in above example(E.g.VI).

Application Development and Execution

Now let's start the development. Before we start, I am assuming you have the required setup in your machine. And for your knowledge I am using the below versions for the development of attached sample.

  • Microsoft Visual Studio 2008
  • Microsoft BizTalk server 2009

You can even migrate into the higher version. Now let’s start with schema design in Biztalk,

Input Schema

Image 1

Figure 1: Input Schema 1

Please note both input and output file types are of the flat file type and each field has a change in property as shown below:

Child Field ElementProperties Value
DateTime Data Type xs:string
  Max Occour *
   Min Occour 0
   Min Occour 0
UTCDateTime Data Type xs:dateTime
  Max Occour *
  Min Occour 0
DateTimeOffSet Data Type xs:string
  Max Occour *
  Min Occour 0

Output Schema

Image 2

Figure 2 : Input Schema 1

In the above schema,

Child Field ElementPropertiesValue
All Fields Data Type xs:string
 Max Occour *
 Min Occour 0

Now please download the folder for the sample input file for map testing. The sample input file you can even generate from source schema. In the solution you will see the below mapping file with many functoids.

Image 3

Figure 3: Map File In Sample Application 1

We will be covering one by all functoids in the below topics.

Image 4

I. Existing Datetime/ Related Functoids

Now, to achieve all the scenarios in attached file we will start with the existing functoids. We have the below functoids in the list of Date/ Time Functoids section.

Figure 1.1 Date/Time Functoids

If we look into the list we see we only have four functoids and we can relate with many other functoids with the existing ones as per our requirement and need.

  • Date and Time gives the current system datetime.
  • Time which returns the current system time.
  • Date which returns the only system date excluding time.
  • Add Days, Where we can add the number of additional days in the existing.

We have one more functoid which checks whether the input value is a valid datetime value or not, which is in Figure 1.2 which is part of logical functoids

Image 5

Figure 1.2 Logical Date

Now let's look into the below scenario (1), "To check the specified input string value is valid datetime or not and pass/display resultant value in output file if it is valid."

To achieve this, first we will take the logical date and value mapping on the page and do the mapping as below. If the value is valid then the logical functoid will pass True the as input value to the value mapping functoid, and the value mapping functoid will consider to pass actual input datetime value to destination Datetime field and if it receives False as input then the field will not be visible on output file.

Image 6

Figure 1.3 Scenario I

In the mapping file (.btm) properties, go to TestMap Input instance and add the input file before you build and test it. Since we are passing the valid input value in the attached solution,the output file will contain the datetime value.

II. Using C# script functoid for Datetime validation Extension

Now let's look for scenario (II) where we will convert the input value to UTC/GMT datetime value. Please do the mapping as below,

Image 7

Here we will use the script functoide to extend the validation. Now if the datetime is valid then we will convert the datetime to UTC/GMT datetime or will not show any value in destination file. And in the script functoid we will write a script for UTC conversion. For that, right click on the script functoid and select configure functoid script and first select the script type. Here we are using the C#, so select C# and then write the below C# code.

C#
public string GetUTCDateTime(string param1)
{
   return Convert.ToDateTime(param1).ToUniversalTime().ToString();
}

Note: I am not following any coding standard, but we can make it better as per required standards/requirement.

III. Consuming C# script DLL in script functoid/mapping app

Now if we look at the scenarios from (3rd to 9th) each require an extension of logic and we must write script within each script functoid. And we must touch the mapping application every time. The better approach is to use an external DLL which has all the script written within it. In case, if any modification required at logical level (other than data /service contract level) we just need to update the logic and deploy the DLL and upgrade in the mapping application but not required to touch the script functoid. The other part is it's very easy to find out errors with VS (as it has a compiler), but we don’t have the option in Biztalk to compile the script within the script functoid.

Now let’s open the Visual Studio

Go To File -->
   New Project -->
            Visual C# -->
                 Select the Class Lib,

Specify the application name (e.g. BiztalkDateTimeScript), location (e.g. D:\Biztalk\Samples) and click ok, now you will be able to see solution with one default class file, named class1.

You can rename the existing or add a new class file with the name BiztalkDateTimeScript.cs, if you planed to rename, please open the file and change the class name inside the cs page. Now add the below method to extract the time from the input dateTime.

C#
public string getTime(string _inputDateTimeValue)
        {
            try
            {
                _inputDateTimeValue = validatedatetimeValue(_inputDateTimeValue);
                return Convert.ToDateTime(_inputDateTimeValue).ToString("HH:mm");
            }
            catch (Exception)
            {

                throw;
            }
        }

Now build the solution and see if it successfully gets built. Once you are done with it add the strong name to the solution and deploy the assembly to GAC. To do it please follow the below link.

Create strong name: http://msdn.microsoft.com/en-us/library/xwb8f617(v=vs.71).aspx

Or you can follow the steps, go to vs studio command prompt -> type CD\ -> type sn –k "specify the name and location of where you want your strong name to get create"

Deploy the assembly into GAC: http://msdn.microsoft.com/en-us/library/dkkx7f79.aspx

In the application/solution properties -> go to singing (which will be last property in VS 2008) , there you choose the assembly and build it. Once you are done, go back again to the VS command prompt and type the below.

Gacutil /I <give the dll with full and correct path>, it will prompt the message  "assembly successfully added to the globle catch."

Now you are all set to use the script DLL into the mapping application. Now desgin your Mapping application as below.

Image 8

Now right click on the second script functoids and select the configuation functoid and you will see the below screen. Note, the first three functiods in the mapper which you have already worked on earlier topics.

Image 9

This time please select the external Library as you are going to use the consumed DLLs script.

Now select the script assembly and you will notice the script DLL will be available in it, which you have included in the application (mostly at first place). Then select the class in script class. As we have class in script class so we will find only one, otherwise choose the requried. Now select the required method in the method dropown. Here I have covered the sample code for scenario III, but for all other scenarios the code is available in the attached folder. To view and execute please download, build and test the map.

Also for scenario (6), the following link may be useful for you: http://www.csharp-examples.net/string-format-datetime/

Expected Error, Exception Caught: The map has empty source and destination schemas.

  • If you miss to add the DLL of class lib.

Output validation error: Root element is missing. / Output validation error: The "any child field" element is invalid

  • If you miss the input file to the map/ invalid path.

Well I am almost done, now it's your turn to come back if any issue with build or some more enhancements required. I will be very happy to work on the scenarios you are looking for and update the article for others to use.

History

Biztalk datetime Functiods Version I

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)