How to use a variable in "{fieldName*C} "when using ObjectScript Trigger Code
Hi,
when I using ObjectScript Trigger Code , I want to get whether the fields has changed by using "{fieldName*C}" , actually, fieldName in "{fieldName*C}" is a real field name ,but in my code, fieldName is a variable as follows:
SET stat=##class(%SYSTEM.SQL).GetColumns(tableName,.byname,.bynum,1)
IF stat=1{
SET i=1
WHILE $d(bynum(i)){
SET xColName=bynum(i)
SET valComp={xColName*C}
IF valComp=1{
SET oldVal=1 //{xColName*O}
SET newVal=2 //{xColName*N}
&sql(INSERT INTO Yan.LogTableExt(Parref,ProperName,OldValue,NewValue) VALUES (:id,:xColName,:oldVal,:newVal) )
}
SET i=i+1
}
}
So, how do I right use xColName in {xColName*C}?
}
It's possible to do this by using a trigger generator. Then you can run GetColumns at compile time of the class, and use the result to write out lines of code using the {fieldName*C} syntax. Just as a warning, using generators can be tricky because it adds a layer of indirection to your code. The best way to debug is to use the "View Other" command in Studio or VS Code and look directly at the generated code.
Here is some sample code for a trigger generator:
Thank you Barton! I will try it.