Distinguished Fields and Optional Elements#

Thought this might be of interest to a few BizTalk developers out there.

It's quite common practice to use Distinguished Fields in orchestrations, to get/set the value of an element or attribute.

However, if the element you're trying to set/get doesn't exist, then this poses a few problems.

Setting a Distinguished Field
If the element/attribute doesn't exist in the target message, then you'll get an exception, no questions asked – there's no way around this.
Under the covers, the SetDistinguishedField method is called to set the value – and it doesn't check if the element/attribute exists first.
The same thing happens if you use the xpath() function to set a value, and the element/attribute doesn't exist – BizTalk isn't about to modify your message and add the element/attribute for you.

In this case, it's your responsibility to check that the element/attribute exists.
If it's a message that you create, then you have to make sure that the element/attribute is created (do this in a map by mapping an empty Value Mapping functoid to it, or setting the Value property to "<empty>").

Getting a Distinguished Field
This is more interesting.
In a nutshell, if the Distinguished Field is of type string (or any other nullable type) then you'll get null.
Otherwise, you'll get an InvalidCastException.

The reason for this is in the underlying code.

The orchestration calls XSDPart.GetDistinguishedField to get your value:
public override object GetDistinguishedField(string dottedPath)
{
    XsdDistinguishedFieldDefinition dFDef = (XsdDistinguishedFieldDefinition) base.GetDFDef(dottedPath);
    object val = base._getXPath(dFDef.XPath, dFDef.IsDynamic);
    if (val == null)
    {
        return null;
    }
    if (val.GetType() != dFDef.FieldType)
    {
        val = XmlHelpers.ChangeType(val, dFDef.FieldType);
    }
    return val;
}

This method will return null if the element/attribute doesn't exist otherwise it will attempt to convert (cast) the value to the appropriate type.

However, the InvalidCastException arises from your orchestration.
Let's assume you had a Distinguished Field called isSuccessful which is of type boolean.

When you write something like
if (Response.isSuccessful)
in an expression shape, the code generated is this:

if (!((bool) __ctx2__.__Response.part.GetDistinguishedField("isSuccessful")))

Ahah! The orchestration does an explicit cast of the Distinguished Field value to a bool.
And an explicit cast of null to a bool will raise an InvalidCastException.

However, an explicit cast of null to a string is fine.

So if you had a Distinguished Field called employeeName of type string, then the same code would be:
if (!((string) __ctx2__.__Response.part.GetDistinguishedField("employeeName")))

And no exception would be raised.

Conclusion
If you're using Distinguished Fields with types other than string, and the element/attribute you're referring to is optional then you must check that the element/attribute exists before attempting to set/get a value.

In most cases, you'd do this by using the xpath() function in an orchestration
(e.g. getting a count of the element/attribute, and checking that count > 0)

Just FYI....!

Thursday, November 15, 2007 3:09:42 PM (GMT Standard Time, UTC+00:00) #    Comments [0]  |  Trackback Tracked by:
"http://morningside.edu/mics/_notes/pages/cymbalta/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/accutane/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/lipitor/index.html" (http://morningsid... [Pingback]
"http://blastpr.com/wiki/js/pages/soma/index.html" (http://blastpr.com/wiki/js/p... [Pingback]
"http://morningside.edu/mics/_notes/pages/ultram/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/melatonin/index.html" (http://blastpr.com/wiki... [Pingback]
"http://morningside.edu/mics/_notes/pages/prilosec/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/lipitor/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://blastpr.com/wiki/js/pages/lexapro/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://morningside.edu/mics/_notes/pages/rainbow-brite/index.html" (http://morn... [Pingback]
"http://morningside.edu/mics/_notes/pages/celebrex/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/cialis/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/cialis/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/prozac/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/viagra/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/coumadin/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/celexa/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/synthroid/index.html" (http://blastpr.com/wiki... [Pingback]
"http://blastpr.com/wiki/js/pages/ultram/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/prilosec/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://blastpr.com/wiki/js/pages/clomid/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/celebrex/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://blastpr.com/wiki/js/pages/tramadol/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/nexium/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/soma/index.html" (http://morningside.e... [Pingback]
"http://blastpr.com/wiki/js/pages/claritin/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://blastpr.com/wiki/js/pages/wellbutrin/index.html" (http://blastpr.com/wik... [Pingback]

 

All content © 2020, Daniel Probert
On this page
This site
Calendar
<September 2020>
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 2.3.12105.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: