Monday, 11 January 2016

Update of Dependant Fields

Hi Everyone,

Today I want to share with you one scenario when you have dependent field - one field is depend on the value of another field and should be recalculated on change of primary field. But you also have user interface where user can change dependent field as well.
So you may have situation when user has changed dependent field and business logic has to recalculate the same field also. By default system will keep user changes as a more important comparing to default changes.
But in this article you will learn, how to override this process and make business logic changes more important than users.

Here you can find an image that illustrated scenario above.



To understand how to handle this situation you have to know how system handle update operation.
When user interface sends update operation to server, it collects all changes that have been done by user since the last call to server. This collection of changes will be processed by server field by field (field sequence as the same as in the DAC), so from update operation of the first field you can cancel or change update operation of the rest of fields.

Full sequence of update scenario you can find here:


To override the logic described above you can use special method from PXCache object: cache.SetValuePending<Field>(row, value);
This method will clear (or change) the value that is stored in collection of values to update (pending values).

Here is shown example where you have 2 dependent fields: UnitPrice and DiscPrice. On update operation of UnitPrice we are clearing value that has been sent from UI, so system will run defaulting logic for DiscPrice field and keep this value for future use.

protected virtual void SOLine_CuryUnitPrice_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e)
{
   sender.SetValuePending<SOLine.curyDiscPrice>(e.Row, PXCache.NotSetValue);
}

PXCache.NotSetValue means that this value is not set bu user interface and has to be skipped. You may provide a real value here, than system will run update operation the same way if this value was sent from user interface.

Thank you. Have a nice development!

No comments: