How use CacheAttached
The CacheAttached event is an Acumatica event that triggers when the cache of a particular DAC is initialized. It allows you to modify or extend the attributes of any field within that DAC, typically for a specific screen or process, without modifying the DAC itself. The main advantage of CacheAttached is that it allows developers to change the behavior of fields in standard DACs without changing their source code.
public virtual void _(Events.CacheAttached<DAC.Field> e)
{
}
PXMergeAttribute is usually used with CacheAttached. It contains a Method property with three values :
1. Replace (Replaces all existing attributes with the ones you define.
Use it when you want to completely redefine the field's attributes and behavior)
2. Merge (Combines new attributes with the existing ones, ensuring the original attributes are retained)
Use when you want to modify or extend the field’s behavior without losing existing attributes)
3. Append (Adds new attributes without modifying or replacing any existing attributes. Use when you want to add additional behavior without changing any existing attributes)
For example, I want to change the basic LocationActiveAttribute attribute. I need something like code below.
[PXMergeAttributes(Method = MergeMethod.Append)]
[PXRemoveBaseAttribute(typeof(LocationActiveAttribute))]
[LocationActive(typeof(Where<Location.bAccountID, Equal<Current<CROpportunity.bAccountID>>, And<MatchWithBranch<Location.cBranchID>>>), DescriptionField = typeof(Location.descr))]
protected virtual void _(Events.CacheAttached<SOSiteStatusFilter.customerLocationID> e)
{
}
Components:
1. [PXMergeAttributes(Method = MergeMethod.Append)]: This attribute ensures that the existing attributes of the customerLocationID field are preserved while adding new ones or overriding certain base attributes.
2. [PXRemoveBaseAttribute(typeof(LocationActiveAttribute))]: This removes the default LocationActiveAttribute applied to the customerLocationID field.
3. [LocationActive]: After deleting the base attribute, I want to add this attribute with some conditions.
4. Cache Attached: used to customize the DAC field (customerLocationID) without directly modifying the DAC itself.