Search code examples
intersystems-cacheintersystemsintersystems-cache-studio

Update value of other column on update of this column


i have question, how to do something like a trigger in Intersystems Cache. Situation: for example i have table X with properties(columns) valueA,valueB i Want to update valueB when valueA changed by UPDATE. I have define global variable ^VALUEBGENER and use to increment it $SEQ function, My Idea was:

Class User.X Extends %Persistent [ ClassType = persistent, DdlAllowed, Final, Owner = {_SYSTEM}, ProcedureBlock, SqlRowIdPrivate, SqlTableName = X]
{
    Property VALUEA As %Library.String(MAXLEN = 8) [ Required,SqlColumnumber = 1];
    Property VALUEB As %Library.Integer(MAXVAL = 2147483647, MINVAL = -2147483648) [ Required,SqlColumnNumber = 1,SqlComputed,SqlColumnumber = 2, SqlComputeCode = {SET {valueB}=$SEQ(^VALUEBGENER)}, SqlComputeOnChange = %%UPDATE];
}

but it's doesnt work, when i change valuea but it works when i change valueb so, any idea? P.S. Sorry for bad english


Solution

  • Can do it by adding a trigger, and SqlCompute


        Class User.X Extends %Persistent [ ClassType = persistent, DdlAllowed, Final, Owner = {_SYSTEM}, ProcedureBlock, SqlRowIdPrivate, SqlTableName = X]
        {
            Property VALUEA As %Library.String(MAXLEN = 8) [ Required, SqlComputed,SqlColumnumber = 1];
            Property VALUEB As %Library.Integer(MAXVAL = 2147483647, MINVAL = -2147483648) [ Required,InitialExpression=0,SqlColumnNumber = 2, SqlComputeCode = {SET {*}=$SEQ(^VALUEB)}, SqlComputeOnChange = %%UPDATE ];
            Trigger[Event=Update]{
               NEW valuebx
               new rowid
               set rowid={ID}
               SET valuebx= 0
               // {fieldname*C} evaluates to 1 if the field has been changed and 0 
               if({VALUEA*C}=1){
                 // we trigger sql computeCode and inc of global variable by update
                 //and it doesnt matter what is here in :valuebx
                   &sql(update x set valueb=:valuebx WHERE ROWID=:rowid)
               }
            }
        }