PXUIFieldAttribute.SetError doesn't work on selector field
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?
Page 1 / 1
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)    {       }
Â
@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.
Hi @aaghaei, I tried adding persistingcheck for field VolumeUOM by this code
[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?
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Â
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."
Please share your Graph Ext for CacheAttached
Hi @aaghaeiÂ
Please refer below,
namespace PX.Objects.IN {          PXNonInstantiatedExtension]    public class IN_InventoryItem_ExistingColumn : PXCacheExtension<PX.Objects.IN.InventoryItem>    {       #region WeightUOM         tPXMergeAttributes(Method = MergeMethod.Append)]       gPXCustomizeBaseAttribute(typeof(INUnitAttribute), "Required", true)]       tPXCustomizeBaseAttribute(typeof(INUnitAttribute), "PersistingCheck", PXPersistingCheck.NullOrBlank)]          public string WeightUOM { get; set; }       #endregion    } } Â
Â
This is the DAC Ext. Please share your Graph CacheAttached Ext.
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   }
Â
 ​
hello @aaghaei, could you please take a look? I can’t find the solutions yet
Hi @mrthanhkhoiÂ
I would suggest creating an Acumatica support ticket for this issue, and once it is resolved, please post the resolution here.
@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:
The SetError method: You are setting the error message to null. It should be the actual error message.
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.
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Â
Hi @mrthanhkhoi were you able to find a solution? Thank you!
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.
@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:
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.