Default Values
You can set the default values to data access class (DAC) fields by using the attributes listed in the following table.
Attribute | Description |
---|---|
PXDefault | This attribute sets the default value and validates the field value when the value is
saved to the database. The following attributes are derived from the
PXDefault attribute:
|
PXDBDefault | This attribute sets the default value by using the value of some source field, and updates the value if the source field value changes in the database before the data record is saved. |
PXDefault Attribute
The PXDefault attribute provides the default value for a DAC field. The default value is assigned to the field when the cache raises the FieldDefaulting event. This happens when a new row is inserted in code or through the user interface.
A value specified as a default can be a constant or the result of a BQL query. If you provide a BQL query, the attribute executes it on the FieldDefaulting event. You can specify both a constant and a BQL query; the attribute first executes the BQL query and then uses the constant if the BQL query returns an empty set. If you provide a DAC field as the BQL query, the attribute retrieves the value of this field from the Current property of the cache object. The attribute uses the cache object of the DAC type in which the field is defined.
The PXDefault attribute also checks that the field value is not null before saving a record to the database. You can adjust this behavior by using the PersistingCheck property. Its value indicates whether the attribute should verify that the value is not null, verify that the value is not null or a blank string, or not perform any verification.
The attribute can redirect the error that happened on the field to another field if you set the MapErrorTo property.
You can use the static methods of the attribute to change the attribute properties for a particular data record in the cache or for all data records in the cache.
Differences
You usually set the default value to a DAC field by using the PXDefault attribute. You can set a constant as the default value or provide a BQL query to obtain a value from the database or data records from the cache. The default value is assigned to the field when a data record that includes this field is inserted into the cache.
You can use the PXDefault attribute just to make the field mandatory for input by using the attribute without parameters.
The PXDefault attribute is not suitable when the default value is retrieved from a field that can be auto-generated by the database (such as the identity field). In this case, you should use the PXDBDefault attribute. It updates the value assigned to the field as the default with the value generated by the database.
For example, if you implement a master-detail relationship, you should use the PXDBDefault attribute to bind the detail data record fields to the master data record key fields. If the master data record is new, its identity field is set to a real value by the database when the master record is saved. So if a detail data record is created before the master data record is saved, the detail data record field is set to the temporary value of the master identity field. However, the PXDBDefault attribute replaces the temporary value with the real value when the detail data record is saved to the database.
You can use the PXUnboundDefault attribute to set the default value to an unbound field. The value is assigned when a data record is retrieved from the database (on the RowSelecting event).