Solved

Avoid Save after validation fails.

  • 19 October 2023
  • 2 replies
  • 107 views

Userlevel 3
Badge

Hi All,

I need to avoid the data saving once validation fails. I wrote a method in RowPersisting event. Validation message comes but data is going to save .So I need to return from the saving until validation sucess.

Here is my code.

protected void _(Events.RowPersisting<HMRCAPIConfigDetail> e)
        {
            HMRCAPIConfigDetail row = e.Row;
            if(row != null)
            {
                if(String.IsNullOrEmpty(row.Description))
                {
                    string msg = "Description Cannot be Empty";                    
                    WebDialogResult result = HMRCAPIConfigDetails.Ask(ActionsMessages.Warning, msg, MessageButtons.OK, MessageIcon.Warning, true);
                    return;
                }                
            }            
        }

Thanks 

icon

Best answer by sweta68 19 October 2023, 11:32

View original

2 replies

Userlevel 7
Badge +10

Hi @bhagyat25 ,

Just add e.Cancel = true; to your code after warning message which will prevents the data saving.

Or else you can use below code snippet to display error message.

 throw new PXSetPropertyException(msg, PXErrorLevel.RowError);

Regards,

Sweta

Hi @bhagyat25 ,

Just add e.Cancel = true; to your code after warning message which will prevents the data saving.

Sweta

No it won’t prevent it. What it will do is rollback any changes made. If that is not something desired (it’ll nuke any modified/added data) the following may work better instead of RowPersisting event:

 

[PXOverride]
public void Persist(Action baseMethod) {

    var cancel = false;    

    // Do your validation setting the ‘cancel’ to true if it fails

    if ( !cancel )

        baseMethod();

}

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