PXDefault vs. PXDBDefault

As we settle into the new normal, whatever that is, we may find ourselves knocking the rust off and doing things we haven’t done much in a while. In my case, the last week brought me the need to create a new set of DAC’s with a parent/child relationship and summing a field in the child record into a field in the parent. Although I visited a lot of the updated training material while much of the world stood still over COVID-19, there is still no substitute for practice to make it all sink in.

When creating a new DAC, I use PXDefault quite a bit. Set a bool field to false by default. Set a decimal to TypeCode.Decimal, “0.0”, etc. When setting up a parent child relationship, it is easy to fall back to PXDefault, but you will quickly notice anomalies in the behavior of your code. The help on PXDBDefault when hovering over the attribute states “Sets the default value for a DAC field. Use to assign a value from the auto-generated key field.” Basically, PXDefault allows setting a default value to “some value” whereas PXDBDefault sets a value to another field from a database record.

In the parent DAC, we define our key field normally. In this case, I’m using a numbering sequence to set a BatchCD field, but the key that is shared between parent and child is the BatchID field.

#region BatchID
[PXDBIdentity]
public virtual int? BatchID { get; set; }
public abstract class batchID : PX.Data.BQL.BqlInt.Field<batchID> { }
#endregion

In the child DAC, we need to define the same field, but we must also define the relation to the parent using PXParent and use PXDBDefault to default the value from the parent DAC.

#region BatchID
[PXDBInt(IsKey = true)]
[PXDBDefault(typeof(MyPrentDAC.batchID))]
[PXParent(typeof(SelectFrom<MyPrentDAC>.Where<MyPrentDAC.batchID.IsEqual<MyChildDAC.batchID.FromCurrent>>))]
public virtual int? BatchID { get; set; }
public abstract class batchID : PX.Data.BQL.BqlInt.Field<batchID> { }
#endregion

Of course, we still must include things like LastLineNbr with a PXDefault(0) in the parent to keep track of the line number to use when creating the next child DAC as well as the PXLineNbr attribute in the child.

Bonus: PXFormula

I can’t say enough how much I like the new training material. Not only do I learn something new every time I revisit it, there are great examples of how to do common activities. The PXFormula attribute is a pretty powerful attribute, but we commonly need to simply sum the value in all the child records and store in the parent. I must be getting old, because the rust seems to settle in faster these days, and I couldn’t remember the right syntax. Lucky for you (and me) it’s in the training material. Here’s my PXFormula to sum my Quantity field in the child DAC into the TotalQuantity field of the parent DAC.

#region Quantity
[PXDBDecimal()]
[PXDefault(TypeCode.Decimal, "0.0", PersistingCheck = PXPersistingCheck.Nothing)]
[PXUIField(DisplayName = "Quantity")]
[PXFormula(null, typeof(SumCalc<MyParentDAC.totalQuantity>))]
public virtual Decimal? Quantity { get; set; }
public abstract class quantity : PX.Data.BQL.BqlDecimal.Field<quantity> { }
#endregion

Since I have created most of my custom DAC’s some time ago, I’m sure to need the reminders again to use PXDBDefault when making my next parent/child relationship. While this post should help with a quick reminder, you should refer back to T210 Customized Forms and Master-Detail Relationship on Acumatica Open University for a complete training guide.

Leave a Reply