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 ?

  • 0
  • 0
  • 77
  • 3
  • 1

Answers

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...

Hi John Kumpf ,

In my case this field cannot be kept as a calculated. I need this to be unique and persistent.

Thanks

Okay, so if I'm understanding you correctly, even if the user field changes after the initial insertion of the object, the userid field should stay the same?

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 ];