I have created a new custom field(postal code - Usrpostalcode) in Sales Order screen and I am trying to make this field required(not working even after adding [PXDefault] [PXUIField(..., Required = true)] ),validate it and make sure that it matches with the Postal code in the Shipping Settings.
Can anyone help me with this?
Getting this error while creating shipment on sales order screen
Adding PXDefault attribute should be enough to make the field required. PXDefault will prevent saving if the value is null or empty. It will raise an error and highlight the field.
Adding the custom field in SOOrder DAC:
Adding the custom field to Sales Order screen:
Testing required field by saving without providing Postal Code value:
Using Inspect Element, locate the field you want to validate against:
In the code section, create a Graph Extension for SOOrderEntry where you will put your validations:
Write your validation code in that graph extension:
namespace PX.Objects.SO
{
public class SOOrderEntry_Extension:PXGraphExtension<SOOrderEntry>
{
public const string postalCodeErrorMessage = "Sales Order postal code must match shipping address postal code.";
// Validate just before saving, triggered when graph save function is called
public void SOOrder_RowPersisting(PXCache sender, PXRowPersistingEventArgs e)
{
if (!ValidatePostalCode(sender, e.Row as SOOrder))
{
// Raise field error
PXUIFieldAttribute.SetError<SOOrderExt.usrPostalCode>(sender, e.Row, postalCodeErrorMessage);
}
}
// Validation function
public bool ValidatePostalCode(PXCache sender, SOOrder soOrder)
{
if (soOrder != null)
{
// Get SOOrder custom field Postal Code
SOOrderExt soOrderExt = sender.GetExtension<SOOrderExt>(soOrder);
if (soOrderExt != null)
{
string soPostalCode = soOrderExt.UsrPostalCode;
// Get current shipping address displayed on Sales Order
SOShippingAddress shippingAddress = Base.Shipping_Address.Current as SOShippingAddress;
if (shippingAddress != null)
{
// Special case to handle null values
if (soPostalCode == null || shippingAddress.PostalCode == null)
{
return soPostalCode == shippingAddress.PostalCode;
}
// Compare postal codes
soPostalCode =soPostalCode.Trim().Replace(" ", "");
string shippingPostalCode = shippingAddress.PostalCode.Trim().Replace(" ", "");
return soPostalCode.Equals(shippingPostalCode, StringComparison.OrdinalIgnoreCase);
}
}
}
return false;
}
}
}
When saving or when the custom postal code field lose focus, validation will be triggered: