Search code examples
acumaticaacumatica-kb

How to delete a header record in Acumatica?


I am trying to delete the record from the header, under a condition when the Fabrication Stage = 6. Cancelled field and then pressing the save button, delete the record.

Attached is my code which gives me an error when I try to delete the record.

 protected void INKitRegister_RowPersisting(PXCache cache, PXRowPersistingEventArgs e)
        {

            var row = (INKitRegister)e.Row;
            if (row == null) return;
            var extKit = row.GetExtension<INKitRegisterExt>();            

            if (extKit.UsrFabStage == "06")
            {
                if (Base.Document.Ask("Confirm Delete", "Are you sure?", MessageButtons.YesNo) == WebDialogResult.Yes)
                {
                    PXTimeStampScope.SetRecordComesFirst(typeof(INKitRegister), true);
                    KitAssemblyEntry graph = PXGraph.CreateInstance<KitAssemblyEntry>();
                    INKitRegister dac = graph.Document.Current = graph.Document.Search<INKitRegister.refNbr>(row.RefNbr);                    
                    graph.Document.Delete(dac);
                    graph.Save.Press();
                }
            }
        }

enter image description here enter image description here


Solution

  • I dislike my answer and will most likely submit another when I have mulled the issue over more and have something more elegant.

    Essentially we mark the record in the cache to be deleted as long as it meets the criteria required. My example confirms the record status in the cache and that it does not have a description.

    On save it will then be deleted, though this solution currently requires a refresh of the screen for the record to drop from the UI.

            public class KitAssemblyEntryExtension : PXGraphExtension<KitAssemblyEntry>
            {
                [PXOverride]
                public void Persist(Action del)
                {
                    INKitRegister document = Base.Document.Current;
    
                    //Confirm the cache status of the record is correct for deletion and that criteria is met for the record we want deleted.
                    if(Base.Document.Cache.GetStatus(document) == PXEntryStatus.Updated && string.IsNullOrEmpty(document.TranDesc))
                    {
                        //Marks the record as deleted in the Cache, when base persist is called this record is then deleted.
                        Base.Document.Cache.MarkDeleted(Base.Document.Current);
                    }
                    del?.Invoke();
                }
            }