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.

0
0 503
Discussion (6)0
Log in or sign up to continue

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)

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

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

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

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