Save DAC class in acumatica to DB

Hello everybody,

today I want to note interesting gotcha. Let's say you want to save object to db in Acumatica. You in your code created some DAC item and want to save it to db. And you call Actions.PressSave() method. And you get error. You can get suspection, that maybe you put some wrong data in your DAC class or something like this. 

But the real reason is different, According to T200 Acumatica manual: " You should not invoke the Persist()method on the current graph instance. You can do it only for a graph instance created in a background operation."  It means that you should create instance of Graph, and in that instance call method Persist. 

Here is fragment, which I extracted with the help of reflector:

          ContractMaint instance = PXGraph.CreateInstance<ContractMaint>();
          instance.Clear();
          if (!redirect && !PXAutonumberingInfo.IsDimensionAutonumbered(
(PXGraph)Base, "CONTRACT"))
throw new PXException("Cannot automatically
renew contract when Auto Numbering is off. Please use Renew Contract action on Customer Contracts screen."
); this.RenewExpiring(instance); instance.Save.Press();

Another way to insert some record in db is to use PXDataBase.Insert. Example of usage is the following:

PXDatabase.Insert<APTran>(
                        new PXDataFieldAssign("TranType", PXDbType.NVarChar, apTran.TranType),
                        new PXDataFieldAssign("RefNbr", apTran.RefNbr),
                        new PXDataFieldAssign("ManualPrice", apTran.ManualPrice),
                        new PXDataFieldAssign("LineNbr", apTran.LineNbr));

The first option gives you opportunity to use logic of graph, while second gives you good perfomance.

No Comments

Add a Comment