Lines are shifting with custom fields in the Opportunity Products grid one row down at a customized site.
Background:
We have a customization which provides unit margins and % in the header. PXFormula is used on the DAC for any dependent on a calculation.
Opportunity Products has 4 added fields:
- Last Cost = Last Cost from InventoryItem
- Total Cost = Qty * Last Cost
- Margin = ExtAmt - ExtCost
- Manual Cost, a checkbox to allow manual override of Last Cost
Opportunity has 2 added fields:
- Margin Total = Sum of Margins
- Margin % = Margin Total/Sales Total
Problem:
There is an issue with a customization Opportunities where lines shift one line when a record is copied from an existing Opportunity or when an Excel file is imported.
Existing Record
After Copy/Paste or Import from Excel
Code:
My current code:
public PXSelect<INItemCost,
Where<INItemCost.inventoryID,
Equal<Current<CROpportunityProducts.inventoryID>>>> Cost;
protected void CROpportunityProducts_RowInserting(PXCache cache,
PXRowInsertingEventArgs e, PXRowInserting InvokeBaseHandler)
{
if(InvokeBaseHandler != null)
InvokeBaseHandler(cache, e);
var row = (CROpportunityProducts)e.Row;
if (row == null) return;
var rowExt = cache.GetExtension<CROpportunityProductsExt>(row);
if (rowExt == null) return;
var cost = Cost.SelectSingle();
if (cache.GetValue(row, "usrManCost") == null) return;
if (cost != null && (bool)cache.GetValue(row, "usrManCost") == false)
{
cache.SetValueExt<CROpportunityProductsExt.usrLastCost>(row, cost.LastCost);
}
}
What could be causing this? I have thoughts that the RowInserting event returns 0 for the first line since the PXSelect<> statement returns 0 because InventoryItem is not in cache until the next row.
One potential solution I came up with was using RowInserted. This resolves the issue when using Copy/Paste. However, it causes Import from Excel to miscalculate Total Margin.