Kevin
Kevin

Reputation: 473

Imported/Copied Opportunity shift one line

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:

  1. Last Cost = Last Cost from InventoryItem
  2. Total Cost = Qty * Last Cost
  3. Margin = ExtAmt - ExtCost
  4. Manual Cost, a checkbox to allow manual override of Last Cost

Opportunity has 2 added fields:

  1. Margin Total = Sum of Margins
  2. 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 RecordExisting Record

After Copy/Paste or Import from ExcelAfter 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.

Upvotes: 0

Views: 70

Answers (2)

Kevin
Kevin

Reputation: 473

The answer is because events in this case must be done in the RowSelecting event handler, along with PXConnectionScope().

Brendan was on the right track moving the PXSelect into the handler. My code in the question becomes the below. Also, note use of Required<> versus Current<>.

    public void CROpportunityProducts_RowSelecting(PXCache cache,
        PXRowSelectingEventArgs e)
    {
        var row = (CROpportunityProducts)e.Row;
        if (row == null) return;

        using (new PXConnectionScope())
        {
            INItemCost cost = PXSelect<INItemCost, 
                Where<INItemCost.inventoryID, 
                Equal<Required<INItemCost.inventoryID>>>>.Select(Base, row.InventoryID);
            if (cost != null && (bool)cache.GetValue(row, "usrManCost") == false)
            {
                //decimal dbLastCost = (decimal)cost.LastCost;
                var lstCost = cache.GetValue(row, "usrLastCost");

                if ((decimal)cost.LastCost == (decimal)0.00)
                {
                    cache.SetValueExt<CROpportunityProductsExt.usrLastCost>(row, 
                        cost.LastCost);
                }
            }
        }
    }

Upvotes: 0

Brendan
Brendan

Reputation: 5623

Could be the Current<> on your view is not really the current you need?

What happens if you simply replace the var cost = Cost.SelectSingle() line with the following to use Required<> as pass in the Inventory ID...

INItemCost cost = PXSelect<INItemCost,
    Where<INItemCost.inventoryID, Equal<Required<CROpportunityProducts.inventoryID>>>>
        .Select(Base, row.inventoryID);

Upvotes: 1

Related Questions