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:

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

Blog Post by: Johan Hedberg