0
votes

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

enter image description here

2

2 Answers

0
votes

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: enter image description here

Adding the custom field to Sales Order screen: enter image description here

Testing required field by saving without providing Postal Code value: enter image description here

Using Inspect Element, locate the field you want to validate against: enter image description here

In the code section, create a Graph Extension for SOOrderEntry where you will put your validations: enter image description here

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: enter image description here

0
votes

You can check for the value by either adding a selector or implementing FieldVerifying.

If using a PXSelector by default the selector will throw an error if not found in the backing table.

Alternatively, you can use the fields FieldVerifying event by adding it to the graph extension on sales order like the example below...

public class SOOrderEntryExtension : PXGraphExtension<SOOrderEntry>
{
    protected virtual void SOOrder_Usrpostalcode_FieldVerifying(PXCache sender, PXFieldVerifyingEventArgs e)
    {
        //search for table value...

        // if not found...

        throw new PXSetPropertyException<SOOrder.usrpostalcode>("Invalid postal code");
    }
}