Solved

FieldVerifying event causing unexpected behavior in my grid - programmer ignorance assumed

  • 12 October 2022
  • 3 replies
  • 177 views

Userlevel 6
Badge +3

I have a user table and a grid.  I want to ensure that when the user selects a customer in the From Customer field, that customer id is not already in the To Customer column.

I’m using the FieldVerifying event on the FromCustomerID field to do the work.

This is how I am doing it.  Note that the BAccount line is just so that I can populate the error message with something useful to the user in the error message.

		protected void _(Events.FieldVerifying<ICSMCCustomersToMerge, ICSMCCustomersToMerge.fromCustomerID> e)
{
if (e.Row == null || e.NewValue == null) return;

ICSMCCustomersToMerge row = e.Row;

//see if FromCustomer is in the ToCustomer column
ICSMCCustomersToMerge check = Details.Search<ICSMCCustomersToMerge.toCustomerID>((int?)e.NewValue);
if (check != null)
{
//get the AcctCD so it can be displayed in the error message
BAccount bAccount = SelectFrom<BAccount>.Where<BAccount.bAccountID.IsEqual<@P.AsInt>>.View.Select(this, (int?)e.NewValue);
e.Cache.RaiseExceptionHandling<ICSMCCustomersToMerge.fromCustomerID>(e.Row, null,
new PXSetPropertyException(ICSMessages.FromCustomerInToCustomer, bAccount.AcctCD, PXErrorLevel.Error));

e.Cancel = true;
}
}

At first glance it seems to be working.  If I select AACUSTOMER in the From column, I get the error message that I wanted:

If I go back to the From column and select APOSTELSCH 

Then click Save, I get an error message:

It seems like when I put a “” or a null (I’ve tried both with the same results) in the RaiseExceptionHandling line as the replacement value, the cache thinks the field is still null even if I go back and select a new value (APOSTELSCH).  I don’t want to leave the current value in that field as it will still allow me to save the “bad record” even after I put an error on the field.  

It gets worse.  The grid now shows the original selection AACUSTOMER in the From column and my FieldVerifying handler for the ToCustomerID field fires and shows an error in the To Customer column.

I have another FieldVerifying event on the ToCustomerID field to make sure that you don’t select a value in the To column if the value exists in the From column.  It uses the same methodology as the FromCustomerID FieldVerifying event, just from the other direction.

		protected void _(Events.FieldVerifying<ICSMCCustomersToMerge, ICSMCCustomersToMerge.toCustomerID> e)
{
if (e.Row == null || e.NewValue == null) return;

ICSMCCustomersToMerge row = e.Row;

//see if ToCustomer is in the FromCustomer column
ICSMCCustomersToMerge check = Details.Search<ICSMCCustomersToMerge.fromCustomerID>((int?)e.NewValue);
if (check != null)
{
//get the AcctCD so it can be displayed in the error message
BAccount bAccount = SelectFrom<BAccount>.Where<BAccount.bAccountID.IsEqual<@P.AsInt>>.View.Select(this, (int?)e.NewValue);
e.Cache.RaiseExceptionHandling<ICSMCCustomersToMerge.toCustomerID>(e.Row, "",
new PXSetPropertyException(ICSMessages.ToCustomerInFromCustomer, bAccount.AcctCD, PXErrorLevel.Error));
e.Cancel = true;
}
}

I’ve tried doing this using the FieldUpdated event.  That was not successful.  The FieldVerifying seems to me to be the correct place to be doing this.

Looking for any advice from my fellow coders.

icon

Best answer by Django 13 October 2022, 21:51

View original

3 replies

Userlevel 7
Badge +5

I’m wondering if your grid control has SyncPosition=True?

Userlevel 6
Badge +3

Hi @ddunn  It is set to true.  I’ve never really understood what that does.  When I setup this processing form, I used the RSSVWorkOrderEntry graph / aspx from the training series as a template.  

In the training series, the FieldVerifying event did not “search the current grid” for other columns in other rows.  It only worked with the current row.  I am thinking that since my search is looking at all other rows in the grid, maybe that is the problem. 

Also, since in the exception handling I do not have a “correct” replacement value to put in the field if it is not valid, that might also be a problem.  In the training graph, they replace the “bad value” with a corrected value, not NULL or an empty string.

FYI, I merely need to remove this code and allow users to violate the rules.  The form will still run properly, but there will be lines which will not be able to be merged.  The report output will inform them properly so it is not a big deal.  But I’d like to be proactive and stop the user from doing something stupid like changing one customer number to another, and then changing a different customer to the one that was just changed.  

Userlevel 7
Badge +5

Right - so I checked my notes and for FieldVerifying, I’ve underlined: This event handler is intended for field validation that is independent of other fields in the same data record.

I’d be tempted to do this validation FieldUpdated because it depends on other fields in the same record.

And with regards to SyncPosition, the highlighted record on the grid doesn’t have to related to whatever record is the ‘current’ record in the underlying data view. When you’re using the grid for data entry or if you need to know exactly what record a user has highlighted (e.g. for a PXAction button that is working with a grid row) I think you definitely need to have SyncPosition=True.  If the grid is just displaying data and you don’t need to know what record the user has highlighted, leave it at false.  Why wouldn’t you always set it to True?  There’s overhead with the extra work that the underlying mechanics are doing.

If I’m wrong about that - I’d be happy to be corrected. :)

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved