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.
Best answer by Django
View original