How to Check if Delete was Pressed as there is a MoveNext Error while deleting item

765 views Asked by At

Is there a way to check if the delete action was pressed in Acumatica screen? This is because I have a code for reloading the StockItems screen, and whenever I am deleting an item it gives me the movenext error, but the item is being deleted successfully. Below is my Reloading Code:

[PXOverride]
  public void Persist(Action persist)
  {
   persist();// this will call base Persist();
   InventoryItemMaint grp = PXGraph.CreateInstance<InventoryItemMaint>();
   InventoryItem inv = PXSelect<InventoryItem, Where<InventoryItem.inventoryCD, Equal<Required<InventoryItem.inventoryCD>>>>.Select(grp, this.Base.Item.Current.InventoryCD.Trim());
   if (inv != null && inv.InventoryID.HasValue)
   {
      grp.Item.Current = grp.Item.Search<InventoryItem.inventoryID>(inv.InventoryID);
      throw new PXRedirectRequiredException(grp, "Reloading Item");
   }
  }
2

There are 2 answers

10
Sin On BEST ANSWER

You can follow the method suggested by Yura, OR you can acheive it in other way i think..

You can check the cache status of the record before calling base persist, if the cache status says it is deleted, dont do your redirect.

The code might be as below [NOT TESTED]

[PXOverride]
  public void Persist(Action persist)
  {
    bool isDeletion = false;
    InventoryItem  CurrentItem = this.Base.Item.Cache.Current as InventoryItem ;
    if(this.Base.Item.Cache.GetStatus(CurrentItem) == PXEntryStatus.Deleted) // Helps to check whether the operation is deletion
       isDeletion = true;
   persist();// this will call base Persist();
    if(!isDeletion)
    {
      InventoryItemMaint grp = PXGraph.CreateInstance<InventoryItemMaint>();
      InventoryItem inv = PXSelect<InventoryItem, Where<InventoryItem.inventoryCD, Equal<Required<InventoryItem.inventoryCD>>>>.Select(grp, this.Base.Item.Current.InventoryCD.Trim());
      if (inv != null && inv.InventoryID.HasValue)
      {
      grp.Item.Current = grp.Item.Search<InventoryItem.inventoryID>(inv.InventoryID);
      throw new PXRedirectRequiredException(grp, "Reloading Item");
      }
    }
  }

EDIT :

I found that the above code will not work as the InventoryItem

this.Base.Item.Current // is returning null

and this is your base problem of movenext error, so i suggest you to update your code as follows

[PXOverride]
        public void Persist(Action persist)
        {            
            persist();// this will call base Persist();
            InventoryItem CurrentItem = this.Base.Item.Current as InventoryItem;
            if (CurrentItem != null)
            {
                InventoryItemMaint grp = PXGraph.CreateInstance<InventoryItemMaint>();
                InventoryItem inv = PXSelect<InventoryItem, Where<InventoryItem.inventoryCD, Equal<Required<InventoryItem.inventoryCD>>>>.Select(grp, CurrentItem.InventoryCD.Trim());
                if (inv != null && inv.InventoryID.HasValue)
                {
                    grp.Item.Current = grp.Item.Search<InventoryItem.inventoryID>(inv.InventoryID);
                    throw new PXRedirectRequiredException(grp, "Reloading Item");
                }
            }
        }
0
Yuriy Zaletskyy On

As one of the ways I propose you to add to your DAC field IsDeleted, like this:

[PXBool]
[PXDefault(false)]
public bool? IsDeleted { get; set; }

then attach to RowDeleting(). For example like this:

protected virtual void ShipmentLine_RowDeleting(PXCache sender, PXRowDeletingEventArgs e)
{
    ShipmentLine line = (ShipmentLine)e.Row;
    line.IsDeleted = true;
}

And finally modify your code to following scenario:

[PXOverride]
public void Persist(Action persist)
{
  ShipmentLine  line = ViewShipmentLine.Current;
  if(line.IsDeleted)
  {
      //do some logic which you consider as nesessary
  }
  persist();// this will call base Persist();
  InventoryItemMaint grp = PXGraph.CreateInstance<InventoryItemMaint>();
  InventoryItem inv = PXSelect<InventoryItem, Where<InventoryItem.inventoryCD,Equal<Required<InventoryItem.inventoryCD>>>>.Select(grp, this.Base.Item.Current.InventoryCD.Trim());
  if (inv != null && inv.InventoryID.HasValue)
        {
              grp.Item.Current = grp.Item.Search<InventoryItem.inventoryID>(inv.InventoryID);
  throw new PXRedirectRequiredException(grp, "Reloading Item");
        }
  }

Hope it helps.