Skip to main content
Answer

Cannot save in event

  • September 5, 2023
  • 7 replies
  • 389 views

Forum|alt.badge.img

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;
    }

Best answer by Naveen Boga

@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();
}
}

 

 

 

7 replies

Forum|alt.badge.img+9
  • Semi-Pro III
  • September 5, 2023

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


Naveen Boga
Captain II
Forum|alt.badge.img+19
  • Captain II
  • September 5, 2023

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;
}
}

 

 

 


darylbowman
Captain II
Forum|alt.badge.img+15

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.


Dmitrii Naumov
Acumatica Moderator
Forum|alt.badge.img+7
  • Acumatica Moderator
  • September 5, 2023

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


Naveen Boga
Captain II
Forum|alt.badge.img+19
  • Captain II
  • September 5, 2023

@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?


Dmitrii Naumov
Acumatica Moderator
Forum|alt.badge.img+7
  • Acumatica Moderator
  • September 5, 2023

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


Naveen Boga
Captain II
Forum|alt.badge.img+19
  • Captain II
  • Answer
  • September 6, 2023

@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();
}
}