Implement PXTransactionScope

Sometimes, part of your coding requires you to revert any transactions that were processed in the middle of a transaction.

Acumatica Framework has the function to cope with that. That is using the PXTransactionScope();

As mentioned also in the Blog before by Sergey Marenich (ctrl + click to follow link : https://newasiablog.acumatica.com/2015/11/using-scopes-in-acumatica.html)

PXTransactionScope– this scope will initialize new transaction, so you can wrap-up some changes into transaction, when system will revert all changes together on any exception. Do not forget to call commit for the transaction scope before dispose it.

Sample code snippet below is one of the many ways you can implement the PXTransactionScope. You must call the method ts.Complete(); to commit all your transactions.

      using (PXTransactionScope ts = new PXTransactionScope())
            {
                try
                {
                    APPaymentEntry Graph = PXGraph.CreateInstance<APPaymentEntry>();

                    APPayment CheckData = new APPayment
                    {
                        DocType = APInvoiceType.Prepayment,
                        VendorID = CurrentDocument.EmployeeID,
                        CuryOrigDocAmt = CurrentDocument.DocAmt,
                        APAccountID = GetUser.PrepaymentAcctID,
                        APSubID = GetUser.PrepaymentSubID,
                        ExtRefNbr = CurrentDocument.ReqClassID + "-" + CurrentDocument.RefNbr,
                    };
                    Graph.Document.Update(CheckData);
                    Graph.Actions.PressSave();

                    APRegisterExt GetExt = Graph.Document.Current.GetExtension<APRegisterExt>();
                    GetExt.UsrRequestFundtype = CurrentDocument.FundType;
                    GetExt.UsrRequestRefNbr = CurrentDocument.RefNbr;

                    /*Bypass approval mode */
                    if (Setup.Current.BypassApprovalRequestDocument == true)
                    {
                        Graph.Document.Current.DontApprove = true;
                        List<APRegister> DocumentForRelease = new List<APRegister>();
                        Graph.Document.Update(Graph.Document.Current);
                        Graph.Actions.PressSave();
                        DocumentForRelease.Add(Graph.Document.Current);

                        APDocumentRelease.ReleaseDoc(DocumentForRelease, false);
                    }
                    else
                    {
                        Graph.Document.Update(Graph.Document.Current);
                        Graph.Actions.PressSave();
                    }


                    if (Graph.Document.Current != null || String.Equals(Graph.Document.Current.RefNbr.Trim(), GetNumbering.NewSymbol, StringComparison.OrdinalIgnoreCase) == false)
                    {
                        CurrentDocument.Released = true;
                        CurrentDocument.DocStatus = FXDocStatus.Open;
                        CurrentDocument.CheckRefNbr = Graph.Document.Current.RefNbr;
                        CurrentDocument.DocBal = CurrentDocument.DocAmt;
                        this.Document.Cache.PersistUpdated(CurrentDocument);
                        this.APDocuments.View.RequestRefresh();

                    }
                    else
                    {
                        throw new PXException(ErrorCreatingDocument);
                    }
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    PXTrace.WriteError(ex.ToString());
                    throw new PXException(ErrorCreatingDocument);
                }
            }

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *