Written by

Blockchain & Full Stack Developer, Teacher at DreamTeam, former InterSystems
Question Nikita Savchenko · Nov 12, 2017

How to Get Old %Persistent Object Properties in %OnBeforeSave?

I am trying to make Caché perform certain actions depending on whether a particular property of the object was changed using triggers.

For example, say I have this class:

Class Dummy.Class Extends %Persistent {

Property Name As %String;
Property Visible As %Boolean;

Trigger OnUpdate [ Event = UPDATE/DELETE, Time = BEFORE ] {
    if ({Published*O} = 0) && ({Published*N} = 1) {
        do ..CertainAction({ID})
    }
}

ClassMethod %OnBeforeSave(insert As %Boolean) As %Status [ Private, ServerOnly = 1 ] {
    // ... ?
}

ClassMethod CertainAction(objId) {
    set ^test = "Object ID=" _ objId _ " has just become visible!"
}

}

I was able to make CertainAction executed on any SQL update, but it doesn't work when object is saved from COS, via .%Save(). However, %OnBeforeSave works instead, but I have no idea how can I access previously set properties there. I tried opening an object there in-place, but the properties turned to be all new instead of old ones.

Thank you for any help.

Comments

Dmitry Maslennikov · Nov 12, 2017

Not sure, but maybe property method GetStored can help. It is ClassMethod which accept object Id as the first argument. Full name for the method <Property>GetStored(id)

0
Nikita Savchenko  Nov 12, 2017 to Dmitry Maslennikov

Yes this works! Thank you Dmitry!

0
Nikita Savchenko  Nov 12, 2017 to Gerd Nachtsheim

Thank you Gerd! Paul just posted the same answer a little while ago :)

0
Paul Waterman · Nov 12, 2017

You should be able to get the trigger to work with object %Save:

Trigger OnUpdate [ Event = UPDATE/DELETE, Time = BEFORE ,Foreach = row/object

0
Gerd Nachtsheim · Nov 12, 2017

Tell your trigger that it shall be pulled on SQL + OO events like this

Trigger OnUpdate [ Event = UPDATE,Foreach = row/object ]
{

[...]

}

That should to the trick,  by default it is SQL only.

HTH

Gerd

0