Question
· May 21, 2018

%OnAddToSaveSet - what has changed

Hi community,

what is the best way to determine if a certain property got changed between %OpenId and %Save? Preferably in the %OnAddToSaveSet method.

Thanks,

Jiri

Discussion (7)0
Log in or sign up to continue

You can determine that in run time with:

  • $system.CLS.GetModified(oref) to ger if oref was modified
  • m%property to get if property changed

Here's a simple example of how it all works

 Class User.Person Extends (%Persistent)
{

Property Name As %String(DISPLAYNAME = "Name");

Property Age As %Integer(DISPLAYNAME = "Age");

/// Create one user
ClassMethod Recreate() As %Status
{
  do ..%KillExtent()
  set person = ..%New()
  set person.Age = $random(100)
  set person.Name = $random(100) 
  quit person.%Save()
}

/// do ##class(User.Person).Test()
ClassMethod Test()
{
  $$$QuitOnError(..Recreate())

  set person = ..%OpenId(1)
  do person.ModificationState()

  set person.Age = $random(100)
  do person.ModificationState()

  set person.Name = $random(100)
  do person.ModificationState()
}

/// Show current object state
Method ModificationState()
{
  write "Object modified: ", $system.CLS.GetModified(), !
  write "Name prop modified: ", ..IsNameModified(), !, !
}

/// Get name property modification status
Method IsNameModified() As %Boolean
{
  quit m%Name
}

}

Executing in terminal:

>do ##class(User.Person).Test()
Object modified: 0
Name prop modified: 0
 
Object modified: 1
Name prop modified: 0
 
Object modified: 1
Name prop modified: 1

Note that changing value back would still mark property as modified.

An interesting combination!

But (from Sample.Person):

zNameGetStored(id) public {
Quit $Select(id'="":$listget($g(^Sample.PersonD(id)),2),1:"") }


there isn't that much to win.
If it then ends with %Save() it is kind of a prefetch of the global buffer.

I saw it mostly used instead of  %Open() loading / swizzling the whole object in memory
if just 1 specific property was required.
(A kind of direct global access in disguise) smiley