Hello @mrthanhkhoi ,
To accomplish what you're aiming to do, you'll need to:
- Modify the
SubID of GLTran when a payment (APPayment) is released. - If the modified
SubID doesn't exist, create a new subaccount.
Here's a step-by-step guide:
1. Extend GLTran DAC: First, you'll want to extend the GLTran DAC to include a custom field which will be used to store the original SubID before changing it.
public class GLTranExt : PXCacheExtension<GLTran>
{
#region UsrOriginalSubID
public abstract class usrOriginalSubID : PX.Data.BQL.BqlInt.Field<usrOriginalSubID> { }
[PXInt]
[PXUIField(DisplayName = "Original Subaccount", Visible = false)]
public virtual int? UsrOriginalSubID { get; set; }
#endregion
}
2. Override the RowInserting Event for GLTran:
public class JournalEntry_Extension : PXGraphExtension<JournalEntry>
{
protected virtual void _(Events.RowInserting<GLTran> e)
{
GLTran row = e.Row;
if (row.Module == "AP")
{
var currentSub = (Sub)PXSelectorAttribute.Select<GLTran.subID>(e.Cache, row);
if (currentSub != null)
{
var segments = currentSub.SubCD.Split('-');
if (segments.Length >= 2)
{
var newSubCD = $"{segments[0]}-{segments[1]}-ABCD";
// Check if subaccount exists
Sub newSub = PXSelect<Sub, Where<Sub.subCD, Equal<Required<Sub.subCD>>>>.Select(Base, newSubCD);
if (newSub == null)
{
newSub = new Sub { SubCD = newSubCD };
Base.SubAccount.Insert(newSub); // Insert new Subaccount
}
row.SubID = newSub.SubID; // Set SubID of GLTran to the new or found Subaccount's ID
}
}
}
}
}
This code modifies the SubID when a GLTran record is inserted and it belongs to the AP module (which is the case when releasing payments). It then splits the subaccount code (SubCD), takes the first two parts, and appends "ABCD" to the end. If such a subaccount doesn't exist, it creates a new one with that code.
Note: This solution works for the scenario you've described, but remember that modifying and creating subaccounts can have many implications. Always test thoroughly in a sandbox environment to ensure that there are no unwanted side effects.
Performance-wise, RowInserting vs RowPersisting might not have a massive difference in performance. The key difference is when they are triggered: RowInserting triggers before a record is inserted into the cache, while RowPersisting triggers before a record is saved to the database. The performance hit comes mainly from accessing the database, not from which event you're using. If you're concerned about performance, focus on optimizing database operations.