BizTalk Mapper - How-to create global variables

Listen with webReader
Published 09 June 11 12:20 PM | Johan Hedberg

One of the things that can be fairly useful in BizTalk maps is the ability to create global variables. These can be any .Net class that can be accessed from the map, everything from simple bool or int values to generic classes such as List<string>. You create these from within a c# scripting functoid and can use them from within the same or other scripting functoids.

A good place to create and initialize global variables is in a scripting functoid at the top of the map (or in another standardized location) so you always know where to look for them.

For any functoid to be triggered it needs to be connected to an output node, and it needs to contain (at least) one c# method. Yet the functoid containing your global variable initialization you often do not want creating output. To create a functoid that will initialize your global variables without creating output create a method that return a void. Such a method will not create any output, yet it will – when connected to node in the destination schema - be included in the resulting and called from the xslt. A good practice is to connect this functoid to your Root node, whatever that node might be named.

The following is an example of a map that creates and uses a global variable:

image

The functoids has the following content, from top to bottom:

string myGlobalString = "hello";

void InitGlobals ()
{
// you can do any initialization logic here
}

This code creates a global variable and has an empty method returning void to fulfill requirements yet yield no output. This is really the only snippet needed, the one that shows how to create a global variable, the rest simply show some examples of how to use it.

string GetGlobalString ()
{
  return myGlobalString;
}

This method takes no input, but returns the globally available string that we defined in the first functoid and returns it – just to show that it’s available.

string UpdateAndReturnGlobalString(string s)
{
  myGlobalString = s;
  return myGlobalString;
}

This method takes an input, and then updates the global variable with that input, before returning it. This is just to show that you can do things to your variable – it is not a constant. For example, had this been a collection, you could add items to it.

This post, although something fairly well know within the community of seasoned BizTalk developers, is meant to illustrate how you can think outside the Mapper designer and the xslt box when developing BizTalk maps. I also created it for the purpose of being able to refer to it as I create posts that are more advanced in nature, without having to explain this concept in those posts.

/Johan

Filed under:

Comments

# Ruth Resende said on June 9, 2011 02:20 PM:

Nice post! Amazing! Congratulations.

# Mark Brimble said on June 10, 2011 04:22 AM:

Nice. I have been trying to explain all this to a junior dev and Now I just point him at your blog.

# Jonathan Moons said on February 10, 2012 09:42 PM:

First a Little Background Many years ago now I had to do some grouping with an incoming file in BizTalk

# Anderson Park said on April 3, 2012 06:03 PM:

I've yet to really try this in code, but when you say "global" variable, are we talking something that's within the scope of a single instance of a map, or something like a static variable that is visible to all instances of a map within a BizTalk process (host) instance?

That's pretty important, as you're then talking about thread-safety issues.

This Blog

News

    Messenger

    Twitter Updates

      Follow me on twitter

      Visitors

      Feedburner Subscribers

      Locations of visitors to this page

      Disclaimer

      All material is provided AS IS voiding any thinkable or unthinkable effect it might have for any use whatsoever. There... is that clear enough ;)

      Pages

    Syndication