r/intersystems • • Oct 21 '26

Troubleshooting %DynamicObject in InterSystems IRIS: 7 Common Pitfalls

In an HL7 interoperability production, a BPL Business Process can extract information from an incoming message and store it in a %DynamicObject for use in routing decisions.

Problems arise when the Business Process reads these properties incorrectly. For example, a misspelled property name or incorrect access to a nested object may return an empty value without producing an error. As a result, the routing rule evaluates the wrong value, fails to match the expected condition, and moves to another rule or the default action.

The following seven examples explain common %DynamicObject pitfalls, why they occur, and how to avoid them.

1. Property Names with Underscores

This is the most common silent failure when consuming REST APIs that use snake_case naming. When you write the following: 

Set val = dynObj.patient_id 

ObjectScript does not read patient_id as a single property name. The underscore _ is the string concatenation operator, so this line is read as the value of dynObj.patient concatenated with the string id. Since dynObj.patient does not exist, the result is empty, with no error or warning. 

For example:

// Wrong - returns empty silently
Try {
    Set val = dynObj.patient_id
} Catch ex {
    Set val = ""
}
// Correct - use %Get() for underscore-named properties
Set val = dynObj.%Get("patient_id") 

This is particularly relevant when working with JSON from REST APIs that use snake_case property names.

2. Case-Sensitive Property Names

%DynamicObject  property names are case-sensitive. For example, patient_id, Patient_Id, and PATIENT_ID represent different properties.

// Wrong - wrong case returns empty silently
Set val = dynObj.%Get("Patient_Id")

// Correct - exact case must match what was set
Set val = dynObj.%Get("patient_id") 

A mismatch in capitalization can return an empty value without indicating that the wrong property name was used. Check the original JSON and make sure the property name matches exactly.

3. Accessing Nested Objects

Nested JSON objects become separate %DynamicObject instances. Dot notation does not automatically traverse these nested objects.

Suppose your metadata includes an "address" object containing a "city" property.

// Wrong - fails silently
Set city = dynObj.address.city
// Correct - get the nested object first, then access its property
Set addr = dynObj.%Get("address")
Set city = addr.%Get("city") 
For deeper structures, retrieve each nested object in sequence:
Set patientObj = dynObj.%Get("patient")
Set contactObj = patientObj.%Get("contact")
Set phone      = contactObj.%Get("phone") 

This approach makes each level of the JSON structure explicit.

4. Checking Whether a Property Exists

Using %Get() alone is not a reliable way to check for the existence of a property. A property might be missing entirely or might exist with an empty value. Both situations can produce an empty result when using %Get().

In the Event Log, you will see ward_code logged as "does not exist" because we never set it, while patient_id is correctly found and returned. Use %IsDefined when you need to distinguish a missing property from one that exists but contains an empty value. // Wrong - cannot distinguish missing property from empty value
If dynObj.%Get("patient_id") = "" {
    // Is it missing or just empty?
}
// Correct - explicitly checks if property exists
If dynObj.%IsDefined("patient_id") {
    Set val = dynObj.%Get("patient_id")
} Else {
    $$$LOGINFO("patient_id does not exist - using default")
    Set val = "UNKNOWN"
} 

5. Iterating Over %DynamicObject Properties

An indexed ObjectScript For loop does not enumerate %DynamicObject properties as object keys. Instead, use %GetIterator().

// Wrong - does not work on %DynamicObject
For i = 1:1:dynObj.%Size() {
    Write dynObj.%Get(i), !
}
// Correct - use %GetIterator()
Set iter = dynObj.%GetIterator()
While iter.%GetNext(.key, .val) {
    If $IsObject(val) {
        $$$LOGINFO("Key: " _ key _ " = [object]")
    } Else {
        $$$LOGINFO("Key: " _ key _ " = " _ val)
    }
} 

%GetIterator() lets you retrieve each property name and its value. If a property contains a nested object, the example identifies it as[object]rather than displaying its contents. The same method also works with %DynamicArray, returning numeric indices starting from zero instead of property names.

6. Distinguishing JSON Null from an Empty String

A JSON property containing null is different from one containing an empty string (""). However, reading either value through %Get() can produce an empty result.

Use %GetTypeOf() when you need to identify the underlying JSON type.

Set testObj = ##class(%DynamicObject).%FromJSON("{""nullProp"":null,""emptyProp"":""""}")
// Both return empty from %Get()
Write testObj.%Get("emptyProp"), !   // empty
Write testObj.%Get("nullProp"), !    // empty
// Use %GetTypeOf() to distinguish
Write testObj.%GetTypeOf("emptyProp"), !  // string
Write testObj.%GetTypeOf("nullProp"), !   // null 

%GetTypeOf() can identify types including "string", "number", "boolean", "null", "object", or "array". Use it whenever you need to make a decision based on what type of value a property holds rather than just whether it is empty or not. 

7. Comparing JSON Numbers with Strings

When JSON contains numeric values, %DynamicObject stores them as numbers rather than strings. Comparing a numeric property with a string can result in a condition that does not match.

Set numObj = ##class(%DynamicObject).%FromJSON("{""age"":25}")
// Wrong - string comparison does not match a numeric value
If numObj.%Get("age") = "25" { ... }
// Correct - use numeric comparison
If numObj.%Get("age") = 25 { ... }
// Also correct - force numeric conversion with unary +
If +numObj.%Get("age") = 25 { ... } 

When working with numeric JSON values, use numeric comparisons or the unary + operator for explicit conversion.

Conclusion

%DynamicObject provides a way to store and access dynamic metadata in InterSystems IRIS Business Processes, but incorrect property access can be difficult to diagnose when routing conditions receive empty or unexpected values. Using %Get() consistently, checking property existence and types, and logging values during development can help identify these issues before they affect routing decisions.

🔎 Read the full guide by @Muhammad Waseem:

https://community.intersystems.com/post/business-rules-deep-dive-dynamic-objects-and-property-access-pitfalls-part-2-0

Key Takeaways

  • Prefer %Get() for %DynamicObject property access, especially when working with JSON property names containing underscores or nested structures.
  • Property names are case-sensitive. Verify their exact spelling and capitalization.
  • Use the appropriate methods for different checks: %IsDefined for property existence, %GetTypeOf() for value types, and %GetIterator() for enumeration.
  • Respect JSON value types when evaluating routing conditions, particularly numeric values.
  • Check actual values in the Event Log and Visual Trace when troubleshooting unexpected routing decisions.
1 Upvotes

0 comments sorted by