Solved

Cannot save in event

  • 5 September 2023
  • 7 replies
  • 168 views

Userlevel 3
Badge

PX1043    Only the methods of the PXCache.Persist family can be used to save changes to the database from a RowPersisting event handler

here is my code 

 protected virtual void _(Events.RowPersisting<BOQTask> e)
    {
      BOQTask row1 = (BOQTask) e.Row;
      if (row1.SubID.HasValue)
        return;
      AutoBoqRefMaint instance = PXGraph.CreateInstance<AutoBoqRefMaint>();
      BOQTmpRef boqTmpRef1 = new BOQTmpRef();
      BOQTmpRef boqTmpRef2 = instance.bOQTmpRefView.Insert(boqTmpRef1);
      boqTmpRef2.BranchID = row1.BranchID;
      boqTmpRef2.AutoBOQRef = row1.TaskID;
      boqTmpRef2.Descr = row1.Descr;
      
      instance.bOQTmpRefView.Update(boqTmpRef2);

      //error here
      instance.Actions.PressSave();

 

      BOQTask row2 = (BOQTask) e.Row;
      object newValue = (object) ("000000000" + instance.bOQTmpRefView.Current.AutoBOQRef);
      e.Cache.RaiseFieldUpdating<BOQTask.subID>((object) row2, ref newValue);
      row2.SubID = (int?) newValue;
    }

icon

Best answer by Naveen Boga 6 September 2023, 04:15

View original

7 replies

Userlevel 7
Badge +10

Hi @kevinheng21 ,

You are trying to save changes to the database in a RowPersisting event handler, which is not the correct place for it. You should perform your data manipulation and validation in this event handler, but not attempt to save changes directly to the database.

Instead, you should set the values or perform validation within the RowPersisting event handler and then let the Acumatica framework handle the saving process during the persist operation.

Regards,

Sweta

Userlevel 7
Badge +17

Hi @kevinheng21  Assuming that you are working on a new customization screen. If yes, you can add the VIEW to the customization graph, so that persist action will be taken care.

Please check with the below code and confirm.

 

public class MyCustomGraph : PXGraph<MyCustomGraph>
{

public PXSelect<BOQTmpRef> BOQTmpRefView; //You can create the VIEW

protected virtual void _(Events.RowPersisting<BOQTask> e)
{
BOQTask row1 = (BOQTask) e.Row;
if (row1.SubID.HasValue)
return;
// AutoBoqRefMaint instance = PXGraph.CreateInstance<AutoBoqRefMaint>();
BOQTmpRef boqTmpRef1 = this.bOQTmpRefView.Insert();
//BOQTmpRef boqTmpRef2 = instance.bOQTmpRefView.Insert(boqTmpRef1);
boqTmpRef1.BranchID = row1.BranchID;
boqTmpRef1.AutoBOQRef = row1.TaskID;
boqTmpRef1.Descr = row1.Descr;

instance.bOQTmpRefView.Cache.Update(boqTmpRef2);

BOQTask row2 = (BOQTask) e.Row;
object newValue = (object) ("000000000" + instance.bOQTmpRefView.Current.AutoBOQRef);
e.Cache.RaiseFieldUpdating<BOQTask.subID>((object) row2, ref newValue);
row2.SubID = (int?) newValue;
}
}

 

 

 

Badge +11

RowPersisting is designed to be used to cancel saving in certain conditions. I believe you can use RowPersisted to make changes to other graphs, which happens after the save, but I'm not in front of my cheatsheet at the moment.

Userlevel 7
Badge +5

If you need to save changes through another graph, you should do it in the Persist method override. 

Userlevel 7
Badge +17

@Dmitrii Naumov According to the above code, kevinheng21 is trying to insert the record into the custom table for each record. Will the Persist() override method will work for Custom screen GRID as well?

Userlevel 7
Badge +5

@Naveen Boga  yes it will. Depends on the type of Save button used (see PXSave s PXSavePerRow).

Userlevel 7
Badge +17

@Dmitrii Naumov  Understood. Thanks for the clarification.

 @kevinheng21  Here is the sample example and hope this gives more insight.



public class MyCustomGraph : PXGraph<MyCustomGraph>
{

public PXSavePerRow<GRIDDAC> GRIDDACVIEW;

public override void Persist()
{
// logic here


base.Persist();
}
}

 

 

 

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved