0
votes

I encounter an issue when using PXAction to create new item in Acumatica and appreciate you can give me a help.

I added the custom auto-increment attribute for the "DocumentNbr" field in my "Document" DAC following the Acumatica official document's example "Example 8.2: Creating the Custom AutoNumber Attribute" in the T200 Document as below.

enter image description here

Here is the snippet of code of the attribute setting for the "DocumentNbr" field:

 #region DocumentNbr
        protected string _DocumentNbr;
        [PXDBString(15, IsUnicode = true, IsKey = true, InputMask = ">CCCCCCCC")]
        [PXSelector(typeof(Search<MABUIPDocument.documentNbr>),
              typeof(MABUIPDocument.documentNbr),
              typeof(MABUIPDocument.documentDate),
              typeof(MABUIPDocument.status),
              typeof(MABUIPDocument.vendorID)
              )]
        [AutoNumber(typeof(MABUIPSetup.autoDocumentNbr), typeof(MABUIPSetup.lastDocumentNbr))]
        [PXDefault()]
        [PXUIField(DisplayName = "ID")]
        public string DocumentNbr
        {
            get
            {
                return this._DocumentNbr;
            }
            set { this._DocumentNbr = value; }
        }
        public class documentNbr : IBqlField { }

    #endregion

It is working fine that I can add, edit and delete documents normally as below:

enter image description here

I have a requirement that creates a new item when clicking button, so I created the "Test Creating new item" button including creating new item logic as below, in my understanding, it would show the created item after clicked the "Test Creating new item" button.

   public PXAction<MABUIPDocument> BtnCreatingNew;
    [PXButton(CommitChanges = true)]
    [PXUIField(DisplayName = "Test Creating new item", Visible = true)]
    protected virtual void btnCreatingNew()
    {
        MABUIPDocument row = Documents.Current;
        row.DocumentDesc = "Test" + DateTime.Now.ToString();
        row = Documents.Update(row);
        Actions.PressSave();
    }

The actually circumstance is although the new row has been inserted into the database and will occur if I click the "Next" arrow but the form content of the current view is cleared after clicking the button, I tried many methods like setting "Document.Current = row" and "sender.SetValue(row, fieldName, fieldNewValue)" but the content has been keeping blank after clicking the button whatever I tried. Can you please give me a hint what possible reason caused the issue? Thank you very much!

enter image description here

1

1 Answers

2
votes

Because your ID value is only generated while new document is saved in the database, you must accordingly update PXAdapter's Searches collection with the actual ID value saved in the database:

public PXAction<MABUIPDocument> BtnCreatingNew;
[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "Test Creating new item", Visible = true)]
protected virtual IEnumerable btnCreatingNew(PXAdapter adapter)
{
    MABUIPDocument row = Documents.Current;
    row.DocumentDesc = "Test" + DateTime.Now.ToString();
    row = Documents.Update(row);
    Actions.PressSave();

    adapter.Searches[adapter.Searches.Length - 1] = row.DocumentNbr;
    return adapter.Get();
}