Skip to main content

Hello

I want to override Subaccount field of GLTran when it is generated by Release an payment (APPayment) by the rule: take first 2 parts of existing SubCD then concat with a “ABCD”.

For example: if the Subaccount is 000-00000-0000, it should be replace by Subaccount 000-00000-ABCD.

And I found that there is a case that I can’t find by calculated SubCD, so I need to create new Sub by calculated SubCD (000-00000-ABCD) then set GLTran.SubID to the created one.

How to create new Sub in event RowInserting of GLTran? Is it possible? 

 

p/s: I don’t want to use RowPersisting event because I wonder they could impact the performance when generating a bulk of GLTran but I’m not sure.

Thanks

Try to override ReleaseInvoiceBatchPostProcessing in APDocumentRelease graph.

        /// <summary>
        /// Extension point for AP Release Invoice process. This method is called after GL Batch was created and all main GL Transactions have been
        /// inserted, but before Invoice rounding transaction, or RGOL transaction has been inserted.
        /// </summary>
        /// <param name="je">Journal Entry graph used for posting</param>
        /// <param name="apdoc">Orginal AP Invoice</param>
        /// <param name="apbatch">GL Batch that was created for Invoice</param>

 

You will have the new GL transactions in the je graph, and you can check if you need to create new subaccounts.


Hello @mrthanhkhoi ,

To accomplish what you're aiming to do, you'll need to:

  1. Modify the SubID of GLTran when a payment (APPayment) is released.
  2. 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 = $"{segmentss0]}-{segmentss1]}-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.


Reply