Loading ...

Handling Exceptions in Acumatica: PXException, PXSetPropertyException, and RaiseExceptionHandling

When developing in Acumatica ERP handling exceptions and validation errors properly is crucial for ensuring robust user-friendly applications. Acumatica provides several mechanisms for dealing with exceptions, each suited for different scenarios.

In this article we’ll explore three key exception-handling approaches: PXException, PXSetPropertyException, and RaiseExceptionHandling, let’s compare them and explain when to use each one.

1. PXException

PXException used for handling global errors that affect the overall business logic. When a PXException is thrown, it typically stops the execution of the current process and signals a severe issue that the user or developer needs to address.


When to Use PXException:
  • Use it for global errors that impact the overall workflow or business process.
  • When you need to stop the operation entirely, such as database connection failures, invalid configurations, or unhandled business logic errors.
  • Ideal for exceptions that require intervention and should be surfaced clearly to the user or developer.

2. PXSetPropertyException

PXSetPropertyException is used for field-level validation. Unlike PXException, this does not stop the execution of the entire operation but flags a specific field on the form with an error message. This type of exception is displayed next to the field in the UI, informing the user that the input data is invalid.


In this scenario  if SomeField is null, the system throws a PXSetPropertyException, indicating that this particular field has an invalid value.

When to Use PXSetPropertyException:
  • Use it for field-level validation errors when a specific input does not meet certain criteria.
  • It’s great for scenarios where you want to notify the user without stopping the entire operation. The user can correct the field and continue.
  • Typically used when checking required fields or when specific business logic applies to individual fields on the screen.

3. RaiseExceptionHandling

RaiseExceptionHandling is a method provided by PXCache that allows you to programmatically mark a field as invalid without throwing an actual exception. This method is frequently used in validation events like FieldVerifying or FieldUpdated. It is highly flexible, allowing for sophisticated validation logic without halting the execution flow.


In this case, instead of throwing an exception, RaiseExceptionHandling marks the field SomeField as invalid and displays an error message, but the code continues executing.

When to Use RaiseExceptionHandling:
  • Use it when you want to validate a field and show an error without throwing an actual exception.
  • Commonly used in event handlers like FieldVerifying, RowPersisting, or RowUpdating, where validation logic needs to occur before saving changes.
  • It provides more control and is especially useful in scenarios where multiple fields or rows are being validated simultaneously.

 


Best Practices

·         Use PXException for critical errors that prevent the operation from continuing. For example, if there’s an issue with database integrity, or if required business logic cannot be fulfilled, use this to notify the user and halt the process.

·         Use PXSetPropertyException when you need to perform field-level validation. If a field fails validation (e.g., it’s left empty or contains an invalid value), this is the best approach to notify the user.

·         Use RaiseExceptionHandling when you want to mark a field as invalid programmatically and handle the error message display without actually throwing an exception. This is particularly useful in event-driven scenarios or when performing more complex validation that involves multiple fields or records.

Be the first to rate this post

  • Currently 0.0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5