Skip to main content
Answer

I try to clear error and reset new value in post code filed update

  • May 29, 2025
  • 2 replies
  • 52 views

Forum|alt.badge.img

I try to clear error and reset new value in post code filed update but not clear and not set new value. if input right code it’s ok but I input wrong code, i will raise error message and after I reinput the right code again it’s not clear error and not set new value.

protected virtual void _(Events.FieldUpdated<CRAddress.postalCode> e)
{
    if (e.Row == null || e.NewValue == null) return;

            e.Cache.RaiseExceptionHandling<CRAddressExt.usrDistrict>(e.Row, null, null);

             if (postal.Length == 4)
            {
                string province = postal.Substring(0, 2);
                string district = postal.Substring(0, 4);
                e.Cache.SetValueExt<CRAddress.countryID>(e.Row, "KH");
                e.Cache.SetValueExt<CRAddress.state>(e.Row, province);
                e.Cache.RaiseExceptionHandling<CRAddressExt.usrDistrict>(e.Row, e.Row, null);
                e.Cache.SetValueExt<CRAddressExt.usrDistrict>(e.Row, district);
            }

}

Best answer by Ankita Tayana

Hi ​@vannakheng,

You're using RaiseExceptionHandling correctly to show the error when a postal code is invalid.

But when the user inputs the correct postal code afterward, you're not clearing the previously raised exception.

Also, SetValueExt may not trigger if the value is the same or if the row state is not correct.

use below code snippet.

protected virtual void _(Events.FieldUpdated<CRAddress.postalCode> e)
{
    if (e.Row == null || e.NewValue == null) return;

    var row = (CRAddress)e.Row;
    string postal = e.NewValue.ToString();

    // Clear previous exception always first
    e.Cache.RaiseExceptionHandling<CRAddressExt.usrDistrict>(row, null, null);

    if (postal.Length == 4)
    {
        string province = postal.Substring(0, 2);
        string district = postal.Substring(0, 4);

        try
        {
            e.Cache.SetValueExt<CRAddress.countryID>(row, "KH");
            e.Cache.SetValueExt<CRAddress.state>(row, province);
            e.Cache.SetValueExt<CRAddressExt.usrDistrict>(row, district);
        }
        catch (PXSetPropertyException ex)
        {
            PXTrace.WriteError($"Error setting values: {ex.Message}");
        }
    }
    else
    {
        // Raise validation error
        e.Cache.RaiseExceptionHandling<CRAddressExt.usrDistrict>(
            row,
            null,
            new PXSetPropertyException("Postal code must be exactly 4 digits.", PXErrorLevel.Error)
        );
    }
}

Hope, it helps!

2 replies

Forum|alt.badge.img+7
  • Captain II
  • May 29, 2025

One challenge might be that when you clear the field the value is being set to null instead of a zero length string. So your test for e.NewValue == null is leaving the routine before you can clear the error.


Forum|alt.badge.img+5
  • Jr Varsity I
  • Answer
  • May 29, 2025

Hi ​@vannakheng,

You're using RaiseExceptionHandling correctly to show the error when a postal code is invalid.

But when the user inputs the correct postal code afterward, you're not clearing the previously raised exception.

Also, SetValueExt may not trigger if the value is the same or if the row state is not correct.

use below code snippet.

protected virtual void _(Events.FieldUpdated<CRAddress.postalCode> e)
{
    if (e.Row == null || e.NewValue == null) return;

    var row = (CRAddress)e.Row;
    string postal = e.NewValue.ToString();

    // Clear previous exception always first
    e.Cache.RaiseExceptionHandling<CRAddressExt.usrDistrict>(row, null, null);

    if (postal.Length == 4)
    {
        string province = postal.Substring(0, 2);
        string district = postal.Substring(0, 4);

        try
        {
            e.Cache.SetValueExt<CRAddress.countryID>(row, "KH");
            e.Cache.SetValueExt<CRAddress.state>(row, province);
            e.Cache.SetValueExt<CRAddressExt.usrDistrict>(row, district);
        }
        catch (PXSetPropertyException ex)
        {
            PXTrace.WriteError($"Error setting values: {ex.Message}");
        }
    }
    else
    {
        // Raise validation error
        e.Cache.RaiseExceptionHandling<CRAddressExt.usrDistrict>(
            row,
            null,
            new PXSetPropertyException("Postal code must be exactly 4 digits.", PXErrorLevel.Error)
        );
    }
}

Hope, it helps!