Loading ...

PXDefault attribute bug in Acumatica

Hello, everyone!

Today, I want to discuss an interesting issue I encountered with the PXDefault attribute in Acumatica. This attribute is widely used in Data Access Classes (DACs) to set default values for fields, ensuring data consistency and enhancing user experience. However, I recently came across an unusual behavior that might cause unexpected issues in filtering logic.

When a user clears a value from the filter, the value still persists internally and continues to appear in the generated SQL query. This behavior can lead to incorrect data retrieval and confusion for users who expect the cleared value to be removed entirely.

 

        #region PeriodYear

        [PXString]

        [PXUIField(DisplayName = "Period Year", Visibility = PXUIVisibility.SelectorVisible)]

        [PXDefault(typeof(Search3<MasterFinYear.year,

                 OrderBy<Desc<MasterFinYear.year>>>),

                 PersistingCheck = PXPersistingCheck.Nothing)]

 

        [PXSelector(typeof(Search3<MasterFinYear.year, OrderBy<Desc<MasterFinYear.year>>>))]

        [PXFieldDescription] public virtual string PeriodYear { get; set; }

        public abstract class periodYear : PX.Data.BQL.BqlString.Field<periodYear> { }

        #endregion

Here are my screenshots:



With this implementation, when a user removes the value from the filter, Acumatica does not actually clear the value from the query, causing unintended filtering effects.

The Solution

To resolve this, we can use the FieldDefaulting event to explicitly handle default values when the filter is cleared:

   protected virtual void _(Events.FieldDefaulting<ASPRProjectionFilter.periodYear> e)

        {

            var row = e.Row;

            if (row != null)

            {

                var year =

                    SelectFrom<MasterFinYear>.OrderBy<Asc<MasterFinYear.year>>.View.

                    Select(this).Select(x => x.GetItem<MasterFinYear>()).LastOrDefault();

                e.NewValue = year.Year;

            }

        }

 

By doing this, we ensure that the latest financial year is stored in memory, reducing unnecessary database hits while still keeping the defaulting logic intact.

Conclusion

The PXDefault attribute is a powerful tool, but as demonstrated, it may not always behave as expected in filtering scenarios. If you encounter a similar issue where a cleared value persists in SQL queries, handling it with FieldDefaulting and implementing caching strategies can provide a robust solution.

I hope this helps anyone facing the same issue! Feel free to share your experiences or ask questions in the comments.

Happy coding!

Be the first to rate this post

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