Skip to main content
Answer

PXUIFieldAttribute.SetError doesn't work on selector field

  • June 29, 2023
  • 16 replies
  • 399 views

Forum|alt.badge.img

Hello colleagues,

I am implementing event handler for StockItem to force User must enter value for selector Weight UOM. The field Weight UOM doesn’t have red red icon on the left of label

using the following code:

protected void InventoryItem_RowPersisting(PXCache cache, PXRowPersistingEventArgs e)
{
var row = (InventoryItem)e.Row;
if (row == null) return;
if (e.Operation == PXDBOperation.Insert)
{
if (string.IsNullOrEmpty(row.WeightUOM))
{
var fieldDisplayName = PXUIFieldAttribute.GetDisplayName<InventoryItem.weightUOM>(cache);
var localizedErrorMessage = PXMessages.LocalizeFormat("Field {0} can't be empty", fieldDisplayName);
PXUIFieldAttribute.SetError<InventoryItem.weightUOM>(cache, row, null);
cache.RaiseExceptionHandling<InventoryItem.weightUOM>(e.Row, row.WeightUOM, new PXSetPropertyException(localizedErrorMessage, PXErrorLevel.Error));
}
}
}

I used the same code for field Weight (type: decimal?) and it works fine

Do you have any idea?

Best answer by Josiah Lisle

@mrthanhkhoi I had a similar issue in 23r2 that I submitted a case for, and the issue might be with your namespace. The below code worked for me on a similar field when I had this issue:

using PX.Data;
using PX.Objects.SO;
using PX.Objects.AR;

namespace PX.Objects.SO
{
    public class SOOrderEntry_Extension21 : PXGraphExtension<PX.Objects.SO.SOOrderEntry>
    {
        protected void SOOrder_RowSelected(PXCache sender, PXRowSelectedEventArgs e, PXRowSelected baseMethod)
        {
            baseMethod?.Invoke(sender, e);

            SOOrder order = e.Row as SOOrder;

            if (order != null && string.IsNullOrWhiteSpace(order.ShipVia))
            {
                PXUIFieldAttribute.SetError<SOOrder.shipVia>(sender, order, "Ship Via is required.");
            }
        }
    }
}

The key part was namespace PX.Objects.SO. When I used a different custom namespace, it was not setting the error correctly. Apparently this is an issue that has already been submitted to the acumatica team, but this was what worked for me. I hope this can be helpful for you.

16 replies

jinin
Pro I
Forum|alt.badge.img+11
  • Pro I
  • June 29, 2023

Hi @mrthanhkhoi 

You can add the Required attribute to display the red asterisk symbol. However, the error message is not appearing for the specific field.
 

  The below code is for the extension of the weight UOM field.

   [INUnit(null, typeof(CommonSetup.weightUOM), DisplayName = "Weight UOM",),Required =true]
    protected virtual void InventoryItem_WeightUOM_CacheAttached(PXCache cache)
    {
    
    }

 


aaghaei
Captain II
Forum|alt.badge.img+10
  • Captain II
  • June 30, 2023

@mrthanhkhoi you will need to know there are two pieces to this. 1) making the field mandatory when persisting and 2) visual display.
What drives the persisting requirement is PersistingCheck of PXDefault (if set to nothing won’t check, nullorblank used for string check and null for non-string types). 

what drives the presentation (*) is Required to true in PXUIField or predefined types.


Forum|alt.badge.img
  • Author
  • Jr Varsity II
  • June 30, 2023

Hi @aaghaei, I tried adding persistingcheck for field VolumeUOM by this code

#region WeightUOM
[PXMergeAttributes(Method = MergeMethod.Merge)]

[PXCustomizeBaseAttribute(typeof(INUnitAttribute), "PersistingCheck", PXPersistingCheck.NullOrBlank)]
public string WeightUOM{ get; set; }

However, I am still not able to set error message for this field which is most important requirement.

The default config of this field:

Do you have any idea?


aaghaei
Captain II
Forum|alt.badge.img+10
  • Captain II
  • June 30, 2023

In your graph extension on the WeightUOM CacheAttached event properties set:

PXNergeMethod Method to Merge,

PXDefault PersistingCheck to null and

PXUIField Required to true

if could not make it work share this piece of code so that we can have a look 


jinin
Pro I
Forum|alt.badge.img+11
  • Pro I
  • June 30, 2023

Hi @aaghaei,

Even though we assign the properties to the field, the validation is not being displayed for that specific field. Instead, an error message is thrown upon saving."


aaghaei
Captain II
Forum|alt.badge.img+10
  • Captain II
  • June 30, 2023

Please share your Graph Ext for CacheAttached


jinin
Pro I
Forum|alt.badge.img+11
  • Pro I
  • June 30, 2023

Hi @aaghaei 

Please refer below,

namespace PX.Objects.IN
{
            [PXNonInstantiatedExtension]
    public class IN_InventoryItem_ExistingColumn : PXCacheExtension<PX.Objects.IN.InventoryItem>
    {
        #region WeightUOM    
        [PXMergeAttributes(Method = MergeMethod.Append)]
        [PXCustomizeBaseAttribute(typeof(INUnitAttribute), "Required", true)]
        [PXCustomizeBaseAttribute(typeof(INUnitAttribute), "PersistingCheck", PXPersistingCheck.NullOrBlank)]
            public string WeightUOM { get; set; }
        #endregion
    }
}
 

 


aaghaei
Captain II
Forum|alt.badge.img+10
  • Captain II
  • June 30, 2023

This is the DAC Ext. Please share your Graph CacheAttached Ext.


jinin
Pro I
Forum|alt.badge.img+11
  • Pro I
  • June 30, 2023

Hi @aaghaei ,
I’ve just tried from the customization project screen by using the Customize attribute and the Override on screen level.

The below sample code for overriding on screen level,

namespace PX.Objects.IN
{
    public class InventoryItemMaint_Extension : PXGraphExtension<InventoryItemMaint>
    {
        #region Event Handlers        
        [INUnit(null, typeof(CommonSetup.weightUOM), DisplayName = "Weight UOM",Required =true,PersistingCheck =PXPersistingCheck.NullOrBlank)]
        protected virtual void InventoryItem_WeightUOM_CacheAttached(PXCache cache)
        {

        }
        #endregion
    }

 

 ​


Forum|alt.badge.img
  • Author
  • Jr Varsity II
  • July 7, 2023

hello @aaghaei, could you please take a look? I can’t find the solutions yet


jinin
Pro I
Forum|alt.badge.img+11
  • Pro I
  • July 10, 2023

Hi @mrthanhkhoi 

I would suggest creating an Acumatica support ticket for this issue, and once it is resolved, please post the resolution here.


davidnavasardyan
Jr Varsity I
Forum|alt.badge.img+3

@mrthanhkhoi 

From your code, it appears you're attempting to perform validation on the WeightUOM field during the RowPersisting event, which occurs just before the row is saved to the database.

However, the RowPersisting event may not be the best place for this kind of validation, because the UI might not refresh as expected to reflect the error. Instead, consider using the FieldVerifying or RowSelected events for your validation.

However, if you still want to use RowPersisting, there are two issues that I'd like to point out in your code:

  1. The SetError method: You are setting the error message to null. It should be the actual error message.

  2. The RaiseExceptionHandling method: The second argument should be the value that causes the error. But since you are checking for an empty or null value, you can leave it null or empty.

Here's your corrected code:

 

protected void InventoryItem_RowPersisting(PXCache cache, PXRowPersistingEventArgs e)
{
var row = (InventoryItem)e.Row;
if (row == null) return;

if (e.Operation == PXDBOperation.Insert || e.Operation == PXDBOperation.Update)
{
if (string.IsNullOrEmpty(row.WeightUOM))
{
var fieldDisplayName = PXUIFieldAttribute.GetDisplayName<InventoryItem.weightUOM>(cache);
var localizedErrorMessage = PXMessages.LocalizeFormatNoPrefix("Field {0} can't be empty", fieldDisplayName);
PXUIFieldAttribute.SetError<InventoryItem.weightUOM>(cache, row, localizedErrorMessage);
throw new PXRowPersistingException(typeof(InventoryItem.weightUOM).Name, null, localizedErrorMessage);
}
}
}

I've also included e.Operation == PXDBOperation.Update in the conditional check to make sure the validation also applies when the record is being updated, not just inserted.

Finally, the RaiseExceptionHandling method was replaced with PXRowPersistingException. This exception will interrupt the persisting operation, preventing the system from saving the record to the database.


Forum|alt.badge.img
  • Author
  • Jr Varsity II
  • July 18, 2023

Hello @davidnavasardyan09,

First of all, thank you for your answer. I have tried with your code but the SetError seem like doesn’t work too.

I want to have message next to the label as following if the field WeightUOM is empty

 

p/s: field WeightUOM is only required when inserting record that why I didn’t include  e.Operation == PXDBOperation.Update 


Chris Hackett
Community Manager
Forum|alt.badge.img
  • Acumatica Community Manager
  • August 9, 2023

Hi @mrthanhkhoi were you able to find a solution? Thank you!


Forum|alt.badge.img
  • Author
  • Jr Varsity II
  • August 10, 2023

Hi @Chris Hackett, Unfortunately I am still unable to set Error message for Selector field. It works for other field types like: text, number, decimal but it doesn’t for Selector field.


Forum|alt.badge.img+1
  • Jr Varsity I
  • Answer
  • June 7, 2024

@mrthanhkhoi I had a similar issue in 23r2 that I submitted a case for, and the issue might be with your namespace. The below code worked for me on a similar field when I had this issue:

using PX.Data;
using PX.Objects.SO;
using PX.Objects.AR;

namespace PX.Objects.SO
{
    public class SOOrderEntry_Extension21 : PXGraphExtension<PX.Objects.SO.SOOrderEntry>
    {
        protected void SOOrder_RowSelected(PXCache sender, PXRowSelectedEventArgs e, PXRowSelected baseMethod)
        {
            baseMethod?.Invoke(sender, e);

            SOOrder order = e.Row as SOOrder;

            if (order != null && string.IsNullOrWhiteSpace(order.ShipVia))
            {
                PXUIFieldAttribute.SetError<SOOrder.shipVia>(sender, order, "Ship Via is required.");
            }
        }
    }
}

The key part was namespace PX.Objects.SO. When I used a different custom namespace, it was not setting the error correctly. Apparently this is an issue that has already been submitted to the acumatica team, but this was what worked for me. I hope this can be helpful for you.