Skip to main content
Answer

Why are values resetting to zero and null when press save?

  • December 6, 2024
  • 1 reply
  • 75 views

Forum|alt.badge.img

I created three DAC fields in a custom DAC as follows. When the user is redirected to the screen, those values are automatically filled. However, when I press save, the LotSerialNbr and Quantity fields are reset to empty and save as null in the database. Meanwhile, the  ProductionNbr field, which is PXSelectors, are working fine. 
 

#region ProductionNbr
    [PXDBString(15, IsKey = true, IsUnicode = true, InputMask = "")]
    [PXUIField(DisplayName = "Production Nbr")]
    [PXSelector(typeof(AMProdItem.prodOrdID))]

 

    public virtual string ProductionNbr { get; set; }
    public abstract class productionNbr : PX.Data.BQL.BqlString.Field<productionNbr> { }
    #endregion

 

    #region LotSerialNbr
    [PXDBString(100, IsUnicode = true, InputMask = "")]
    [PXUIField(DisplayName = "Lot Serial Nbr")]
    public virtual string LotSerialNbr { get; set; }
    public abstract class lotSerialNbr : PX.Data.BQL.BqlString.Field<lotSerialNbr> { }
    #endregion

 

    #region Quantity
    [PXDBDecimal()]
    [PXUIField(DisplayName = "Quantity")]
    public virtual Decimal? Quantity { get; set; }
    public abstract class quantity : PX.Data.BQL.BqlDecimal.Field<quantity> { }
    #endregion

How can I solve this?
Any detailed instructions, tips, or code snippets would be greatly appreciated. Thank you!

Best answer by noorula77

Hi ​@RKarunarathne51 ,
Write code on RowPersisting event assign value their value will get save on database.

Here are the key changes and explanations to fix your issue:

  1. Add PXDefault attributes:
    • For required fields, add the [PXDefault] attribute
    • For optional fields, use [PXDefault] with PersistingCheck = PXPersistingCheck.Nothing
    • For numeric fields, provide a default value
      Example:

      #region LotSerialNbr
      [PXDBString(100, IsUnicode = true, InputMask = "")]
      [PXDefault(PersistingCheck = PXPersistingCheck.Nothing)] // Allows null values but ensures field binds properly
      [PXUIField(DisplayName = "Lot Serial Nbr")]
      public virtual string LotSerialNbr { get; set; }
      public abstract class lotSerialNbr : PX.Data.BQL.BqlString.Field<lotSerialNbr> { }
      #endregion

      #region Quantity
      [PXDBDecimal()]
      [PXDefault(TypeCode.Decimal, "0.0", PersistingCheck = PXPersistingCheck.Nothing)] // Defaults to 0.0 for numeric fields
      [PXUIField(DisplayName = "Quantity")]
      public virtual Decimal? Quantity { get; set; }
      public abstract class quantity : PX.Data.BQL.BqlDecimal.Field<quantity> { }
      #endregion
       

  2. Value persistence strategies:
    • Override the Persist method in your graph to ensure values are properly set
    • Add a RowPersisting event handler to validate values before saving
    • Use Cache.GetValueExt to retrieve the actual values from the UI

Example:
In your graph, explicitly set values in the cache during redirection or initialization:

protected void _(Events.RowInserted<MyCustomDAC> e)
{
    var row = e.Row;
    if (row == null) return;

    // Ensure LotSerialNbr and Quantity are initialized
    if (string.IsNullOrEmpty(row.LotSerialNbr))
        row.LotSerialNbr = "DEFAULT_SERIAL";
    if (row.Quantity == null)
        row.Quantity = 1.0m; // Default quantity
}

Use RowPersisting to ensure values are not cleared during save:
 

protected void _(Events.RowPersisting<MyCustomDAC> e)
{
    var row = e.Row;
    if (row == null) return;

    // Debugging
    PXTrace.WriteInformation($"LotSerialNbr: {row.LotSerialNbr}, Quantity: {row.Quantity}");

    // Ensure these fields are valid before persisting
    if (string.IsNullOrEmpty(row.LotSerialNbr))
        throw new PXRowPersistingException(nameof(row.LotSerialNbr), null, "Lot Serial Nbr cannot be empty.");
}
 

Hope it will work.

1 reply

Forum|alt.badge.img+1
  • Jr Varsity III
  • Answer
  • December 6, 2024

Hi ​@RKarunarathne51 ,
Write code on RowPersisting event assign value their value will get save on database.

Here are the key changes and explanations to fix your issue:

  1. Add PXDefault attributes:
    • For required fields, add the [PXDefault] attribute
    • For optional fields, use [PXDefault] with PersistingCheck = PXPersistingCheck.Nothing
    • For numeric fields, provide a default value
      Example:

      #region LotSerialNbr
      [PXDBString(100, IsUnicode = true, InputMask = "")]
      [PXDefault(PersistingCheck = PXPersistingCheck.Nothing)] // Allows null values but ensures field binds properly
      [PXUIField(DisplayName = "Lot Serial Nbr")]
      public virtual string LotSerialNbr { get; set; }
      public abstract class lotSerialNbr : PX.Data.BQL.BqlString.Field<lotSerialNbr> { }
      #endregion

      #region Quantity
      [PXDBDecimal()]
      [PXDefault(TypeCode.Decimal, "0.0", PersistingCheck = PXPersistingCheck.Nothing)] // Defaults to 0.0 for numeric fields
      [PXUIField(DisplayName = "Quantity")]
      public virtual Decimal? Quantity { get; set; }
      public abstract class quantity : PX.Data.BQL.BqlDecimal.Field<quantity> { }
      #endregion
       

  2. Value persistence strategies:
    • Override the Persist method in your graph to ensure values are properly set
    • Add a RowPersisting event handler to validate values before saving
    • Use Cache.GetValueExt to retrieve the actual values from the UI

Example:
In your graph, explicitly set values in the cache during redirection or initialization:

protected void _(Events.RowInserted<MyCustomDAC> e)
{
    var row = e.Row;
    if (row == null) return;

    // Ensure LotSerialNbr and Quantity are initialized
    if (string.IsNullOrEmpty(row.LotSerialNbr))
        row.LotSerialNbr = "DEFAULT_SERIAL";
    if (row.Quantity == null)
        row.Quantity = 1.0m; // Default quantity
}

Use RowPersisting to ensure values are not cleared during save:
 

protected void _(Events.RowPersisting<MyCustomDAC> e)
{
    var row = e.Row;
    if (row == null) return;

    // Debugging
    PXTrace.WriteInformation($"LotSerialNbr: {row.LotSerialNbr}, Quantity: {row.Quantity}");

    // Ensure these fields are valid before persisting
    if (string.IsNullOrEmpty(row.LotSerialNbr))
        throw new PXRowPersistingException(nameof(row.LotSerialNbr), null, "Lot Serial Nbr cannot be empty.");
}
 

Hope it will work.