Skip to main content
Question

How to Resolve Duplicate Loop Execution in Persist Method

  • February 5, 2025
  • 6 replies
  • 81 views

Forum|alt.badge.img

I'm overriding the Persist method in the Invoices and Memos (AR301000) screen.  I'm using a foreach loop to update the unit price of an item. However, the loop is executing twice, resulting in incorrect calculations.  The values before calling baseMethod() are correct, as verified using PXTrace.  Therefore, I suspect the issue occurs during the execution of baseMethod(). I've tried various approaches to resolve this, but none have been successful. How can I fix this?

Any detailed instructions, tips, or code snippets would be greatly appreciated. Thank you!
 

[PXOverride]
public virtual void Persist(Action baseMethod){

PXResultset<ARTran> TransactionLines = PXSelect<ARTran,
    Where<ARTran.refNbr, Equal<Required<ARTran.refNbr>>,
    And<ARTran.tranType, Equal<Required<ARTran.tranType>>>>>
    .Select(this.Base, currentView.RefNbr, currentView.DocType)
        
        var multiplicationFactor = contractExt.UsrAdjustmentDays //Custom field where user enters an integer. I retrieve this value to here
            if (TransactionLines != null){
                    foreach (ARTran TransactionLine in TransactionLines)
                    {
                       TransactionLine.CuryUnitPrice = TransactionLine.CuryUnitPrice * multiplicationFactor;
                       Base.Transactions.Update(TransactionLine);
                    }

            }

baseMethod();
}

6 replies

praveenpo
Semi-Pro II
Forum|alt.badge.img+3
  • Semi-Pro II
  • 99 replies
  • February 5, 2025

Hi ​@RKarunarathne51 ,

try with the below code and see if it works.

[PXOverride]
public virtual void Persist(Action baseMethod){

       
        var multiplicationFactor = contractExt.UsrAdjustmentDays //Custom field where user enters an integer. I retrieve this value to here
            if (TransactionLines != null){
                    foreach (ARTran TransactionLine in Base.Transcations.Select())
                    {
                       TransactionLine.CuryUnitPrice = TransactionLine.CuryUnitPrice * multiplicationFactor;
                       Base.Transactions.Update(TransactionLine);
                    }

            }

baseMethod();
}


Forum|alt.badge.img+1

Hello - difficult to say why the Persist method is being called mulitple times but I think you need to deal with it and adjust the business logic of your code.

The following line…
TransactionLine.CuryUnitPrice = TransactionLine.CuryUnitPrice * multiplicationFactor; 
...will continue to increase the value of CuryUnitPrice over and over again when Persist is called and I often find Persist is call more often than I expect.

You should perhaps look for a way to store the original CuryUnitPrice in a custom field and use something like the following to update CuryUnitPrice.
TransactionLine.CuryUnitPrice = TransactionLine.usrOrigCuryUnitPrice * multiplicationFactor; 

I don’t think your code is causing Perist to be called multiple times. You could comment out the following line and step through the code in visual studio to do sure.
Base.Transactions.Update(TransactionLine);
In anycase, you should look to rework the business logic so that multiple calls of Persist don’t break your calculation.

Good luck
Steve


Forum|alt.badge.img

Hi ​@praveenpo ,
I tried but same result


Forum|alt.badge.img

Hi ​@stephenward03 ,
Actually, there are many conditions that must be met for the above for loop to execute. I didn't include them because it would have made the code too long. The for loop executes correctly twice when all the conditions are met. I can’t find a way to stop it


Forum|alt.badge.img+1

Without sharing the conditions I don’t think anyone can help you further. You cannot stop Persist firing multiple times, the code you have shared will update the CuryUnitPrice multiple times.

If you want to stop your loop from being called mulitple times then it sounds like more conditions are needed. Even so, a better solution will be to rework the business logic as suggested earlier.

 


Forum|alt.badge.img
  • Jr Varsity I
  • 71 replies
  • February 5, 2025

Hi ​@RKarunarathne51 

 

Instead of modifying ARTran records within Persist handle updates inside RowPersisting to ensure the calculation happens only once.

public class ARInvoiceEntry_Extension : PXGraphExtension<ARInvoiceEntry>
{
protected void ARTran_RowPersisting(PXCache sender, PXRowPersistingEventArgs e)
 {
     ARTran row = (ARTran)e.Row; if (row == null) return;
    var doc = Base.Document.Current; var contractExt = doc.GetExtension<YourCustomExtension>();

    // Replace with your extension name

  if (contractExt?.UsrAdjustmentDays != null)

    {
       row.CuryUnitPrice = row.CuryUnitPrice * contractExt.UsrAdjustmentDays;

    }

  }

}

 

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings