Unique Index Open on Empty String
I have a unique index on two properties in a class, and I want to open the instance by index key with one of the two properties is set to the empty string. Is this possible to perform?
Take the example class here
Class TestIndex
{
Property Mandatory as TestClass [Required];
Property Alternate as TestClass;
Property AltOrEmpty As %String [ Calculated, Required, SqlComputeCode = {Set {*} = $Case({Alternate},"":$c(0),:{Alternate})}, SqlComputed ];
Index AlternateMandatory on (Mandatory, AltOrEmpty) [ Unique ];
}
ObjectScriptObjectScript
And example data as
Mandatory | Alternate | AltOrNUL |
2 | ||
2 | 3 | 3 |
I can open the second line in the table as
>set testInstance = ##class(TestIndex).AlternateMandatoryOpen(2,3,.st)
>w st
1
ObjectScriptObjectScript
And I want to be able to use the same functionality to open the first line. This would be as open on one valid Id and on empty string, which results in an attempt to open NULL and fails.
>set testInstance = ##class(TestIndex).AlternateMandatoryOpen(2,"",.st)
>zw st
ERROR #5770: Object open failed because 'AlternateMandatory' key value of '2:NULL' was not found
ObjectScriptObjectScript
I am wondering if there is a syntax that would work to find and open this instance based on the unique index containing the empty string, or not based on the inherent design to have a unique index on an empty string.
In your sample code AltOrEmpty property contains a not null value of Mandatory property or $c(0), so:
set testInstance = ##class(TestIndex).AlternateMandatoryOpen(2,$c(0),.st)
This worked perfectly, thank you!