· May 12, 2017

Calling list.FindOref(<object from indexOpen call>)

Hello All,

I have a question / issue regarding the calling list.FindOref(<object from indexOpen call>)
Here is a simple way to reproduce the issue: 
2 classes: Utility.contacttypes and Utility.person

Class Utility.contacttypes Extends %Persistent
Property description As %Library.String(TRUNCATE = 1);
/// Index for property description
Index descriptionIndex On description [ Unique ];


Class Utility.person Extends %Persistent
Property types As list Of Utility.contacttypes;
/// Date Setup to recreate an error
ClassMethod DataSetup()
    Set ref=##class(Utility.contacttypes).%New()
    Set ref.description="Shipping"
    Do ref.%Save()
    Set ref=##class(Utility.contacttypes).%New()
    Set ref.description="Billing"
    Do ref.%Save()    
    Set tPerson = ##class(Utility.person).%New()
    Do tPerson.types.Clear()
    Set aryContacts(1)=""
    Set aryContacts(2)=""
    Set id=""
    {  Set id=$o(aryContacts(id))
       Do tPerson.types.InsertObjectId(id)
    Do tPerson.%Save()
    Write !,"Created a record"
    Quit 1
/// Reproducing the issue - calling list.FindOref(<object from indexOpen call>) first time returns null!!!
ClassMethod Problem()
    Set tPerson = ##class(Utility.person).%OpenId(1)
    For i=1:1:2 {
        If (tPerson.types.FindOref(##class(Utility.contacttypes).descriptionIndexOpen("Billing"))) {
            Write !,"Run Number:"_i_" - Found it ok"
        } else {
            Write !,"Run Number:"_i_" - ERROR!, I did not find it!"
        Close tPerson
        Quit 1


ALEX>do ##class(Utility.person).DataSetup()

Created a record

ALEX>do ##class(Utility.person).Problem()

Run Number:1 - ERROR!, I did not find it!
Run Number:2 - Found it ok

As you can see the first time I did not find anything, but the record is there and there are no issues with it since second time, same call I do find it ok.
I was hoping someone can shine some light on this issue.  The workaround has been developed, of course, however it is interesting to find out why this is happening in the first place.

Thank you very much for your time,

Discussion (5)3
Log in or sign up to continue

Hi Alex,

What happens if you call %BuildIndices for the "descriptionIndex" of Utility.contactTypes class after adding those 2 entries in DataSetup? I had an issue with a index "Find" call that went away after %BuildIndices on the index. Of course, I wouldn't expect that we'd need to build indices after each time we create a new object, so what I (and maybe we) found is a bug?

More investigation/diagnosis is needed for what I found; thought I'd bring it up in case it was related to what you found.

Thank for posting,