Question
· Feb 12, 2019

How to set a property value with combination of ID and Date on new object

Hi Guys,

I want a field called UserId and it should be formed by ID and Date when it is inserted at first time.

But, unfortunately I am unable to do it with any of call back methods and Computed code since there wont be any availability of %Id().

Is there any possible way to do it ?

Discussion (4)1
Log in or sign up to continue

I'm guessing you're looking for the %%ID special reference.  Something like this:

Class Testing.TestingCalcProps Extends %Persistent
{

Property user As %String;

Property a As %String [ Calculated, SqlComputeCode = {set {*} = {user}_{%%ID}}, SqlComputed];

}

I'd recommend the Calculated keyword, as elements only gain id's after they're saved, which could perhaps lead to inaccurate values in the calculated property before the saving happens.

Here's the resource which talks about these properties: https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY...

And here's the one that mentions %%ID: https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY...

You can remove the Calculated keyword and use a combination of  SqlComputeOnChange and Readonly keywords.

What you accomplish:

- Removing Calculated keyword, makes the column persistent

- Adding SqlComputeOnChange gives you control on when you want the value to be calculated (using %%INSERT or %%UPDATE) and based on which column (don't add any if calc must happen for all inserts regardless of which fields are being inserted).

- Adding Readonly just provides and extra "safety" that the value can't be changed via SQL or Object means.

 Property readonlyprop As %String [ ReadOnly, SqlComputeCode = { {*} = {ID}_(+$H)}, SqlComputed, SqlComputeOnChange = %%INSERT ];