How to customize PXDefault attribute of Acumatica

Sometimes you can get request from BA or client to add new field like this: “On the Customers (AR.30.30.00) screen add a checkbox field called Membership. It is a required field and the default value is False. The Membership field should be already there.”

How we usually develop it:

public class PACustomerExt : PXCacheExtension<Customer>
{
	public static bool IsActive() => true;
 
	#region UsrMembership
	[PXDBBool]
	[PXUIField(DisplayName = "Membership", Required = true)]
	[PXDefault(false)]
	public virtual bool? UsrMembership { getset; }
	public abstract class usrMembership : PX.Data.BQL.BqlBool.Field<usrMembership> { }
	#endregion
}

 

And as you know, this logic will work correct for new records in DB, but when you will change existing records in DB, than you will get nullable Exception because of this custom field and PXDefault attribute. “PersistingCheck = PXPersistingCkeck.Nothing” do not help, it excludes the required status of the custom field.

So we can fix this issue and develop custom Default attribute, and we do not need create graph extension and develop additional logic in events.

Here is example of correct logic according the request:

public class PACustomerExt : PXCacheExtension<Customer>
{
	public static bool IsActive() => true;
 
	#region UsrMembership
	[PXDBBool]
	[PXUIField(DisplayName = "Membership", Required = true)]
	[PXDefaultCustom(false)]
	public virtual bool? UsrMembership { getset; }
	public abstract class usrMembership : PX.Data.BQL.BqlBool.Field<usrMembership> { }
	#endregion
}
 
 
[PXAttributeFamily(typeof(PXDefaultAttribute))]
public class PXDefaultCustomAttribute : PXDefaultAttribute
{
	public PXDefaultCustomAttribute(object value) : base(value) { }
 
 
	public override void RowPersisting(PXCache sender, PXRowPersistingEventArgs e)
	{
		var fieldValue = sender.GetValue(e.Row, base.FieldName);
 
		if (fieldValue == null)
			sender.SetValue(e.Row, base.FieldName, false);
	}
}

 

Also you can customize PXDefault attribute with any logic that you need, one more example:

[PXAttributeFamily(typeof(PXDefaultAttribute))]
public class PXDefaultCustomAttribute : PXDefaultAttribute
{
	public PXDefaultCustomAttribute(object value) : base(value) { }
 
	public override void FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
	{
		base.FieldDefaulting(sender, e); // you can raise base event and logic 
 
		e.NewValue = base._Constant;  // you can setup or check constant value of default attribute
 
		// develop needed logic
	}
 
	public override void RowPersisting(PXCache sender, PXRowPersistingEventArgs e)
	{
		// develop needed logic before value will be persisted to DB
 
		var fieldValue = sender.GetValue(e.Row, base.FieldName);
 
		if (fieldValue == null)
			sender.SetValue(e.Row, base.FieldName, false);
 
		// base.RowPersisting(sender, e);  - you can raise base event if you need, also you can set up PXPersistingCheck on you custom attribute
	}
}

 

Add comment

Loading