In my previous blog, I have gone through on a general approach how to create a computed method which is so straight forward. The current writing is something more bigger. Lets dig deeper.
Problem:
As the key importance of a computed field is to improve performance and also while exporting from a Data Entity, Skip Staging is the step we do more often for faster export where computed field is still needed instead of Virtual field. The difficulty here is when working on converting a display method to a computed field which is having many interlinked methods.
On the same note, I recently received a requirement to build an entity for Cost Objects where this form has many display methods and the client always uses this Skip Staging functionality.
Solution:
I made similar Entity for physical fields using InventSum, InventDim and InventTableModule tables.
The Average Unit Cost is the field which is basically a display method that took some time to investigate and find the right solution.
And finally made the below Computed Field.
I used a select statement and using limited resources like Item and site with some case and Grouping statements.
The idea here is basically to give the Average Unit Cost of that item in that Site, which is equal to total sum of PostedValue and PhysicalValue which is divided by sum of Received Quantity and difference of Posted and Deducted Quantity.
If we have PhysicalValue, PostedValue or Deducted values then we can leverage SysComputedColumn Class methods.
Below is the implementation for the same
The actual display method can be seen at InventCostOnhandItem Form and at InventSum datasource methods with name averageCostPriceUnit(). From the UI, this can be obtained from Released Products > Manage Costs > Cost Objects > Average Unit Cost.
With this we have seen some tricky Switch statement in SQL and then used that in our X++ to write a computed method.
​​​​​​​For better view of the code, try checking in a laptop.
Happy Learning!
PS: The Entity or table names looks like standard but they aren't standard. I just used these names for demo purpose.