From my understanding, non-persisted fields are the best way to handle data that needs to be calculated using some business logic. However, since these fields aren’t saved to the database, we can’t use them in generic inquires and reports. If I instead saved this field to the database, I think I would need to make sure that it is recalculated either every time one of the values in the business logic equation changes, or if a user or anything else is interacting with it, including a report/inquiry. I’m not sure of the best way of doing that or if I’m missing something.
Save field calculated using business logic to database
Best answer by Dmitrii Naumov
Hi
Well, first of all, calculated fields can be used in GIs/Reports. For that you need to implement the calculation on the DAC level, e.g. via PXDBScalar or PXDBCalced attribute.
If the logic is not too complex, it is the recommended way.
If you want to stick to the idea of saving the value to the DB, the recommended way is to add events that trigger the recalculation.
E.g. if the value depends only on the fields of the same record, you can implement FieldUpdated event for each of these fields.
If the value depends on other data records, it is recommended to implement RowInserted, RowUpdated, RowDeleted for each record type it depends on.
The same can be implemented via PXFormula attribute.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.

