首页 文章

Acumatica邮政编码验证和匹配

提问于
浏览
0

我在销售订单屏幕中创建了一个新的自定义字段(邮政编码 - Usrpostalcode),我正在尝试使该字段成为必需(即使在添加[PXDefault] [PXUIField(...,Required = true)]后也无法工作),验证它并确保它与运输设置中的邮政编码匹配 .

谁能帮我这个?

在销售订单屏幕上创建装运时出现此错误

enter image description here

2 回答

  • 0

    添加PXDefault属性应该足以使该字段成为必需 . 如果值为null或为空,PXDefault将阻止保存 . 它会引发错误并突出显示该字段 .

    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

    您可以通过添加选择器或实现FieldVerifying来检查值 .

    如果默认情况下使用PXSelector,则如果在支持表中找不到,则选择器将抛出错误 .

    或者,您可以使用字段FieldVerifying事件将其添加到销售订单上的图表扩展中,如下例所示...

    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");
        }
    }
    

相关问题