PXLongOperation Cleans Everything How To Avoid

 

Hello everybody,

today I want to write a few words about interesting behavior of PXLongOperation.StartOperation in Acumatica.

On the glance that looks pretty simple and straightforward. You have something processing of which will take a long time? Use PXLongOperation.StartOperation. So imagine, you extend ARPaymentEntry graph like this:

public PXAction<ARPayment> StartOperation;
[PXProcessButton]
[PXUIField(DisplayName = "Some long running operation")]
protected virtual IEnumerable startOperation(PXAdapter adapter)
{
   
   PXLongOperation.StartOperation(Base, () => SomeLongRunningOperation(Base.Document.Current));
return adapter.Get(); }

In this case you may notice interesting behavior. If your ARPayment is saved, then everything will work relatively fine. But if not, any kind of data, that user enetered will be lost. How to deal with it. Below goes implementation that will deal with this:

public PXAction<ARPayment> StartOperation;
 
[PXProcessButton]
[PXUIField(DisplayName = "Some long running operation")]
protected virtual IEnumerable startOperation(PXAdapter adapter)
{
    Base.Save.Press();
    PXCache cache = Base.Document.Cache;
    List<ARRegisterlist = new List<ARRegister>();
    list.Add(Base.Document.Current);
    PXLongOperation.StartOperation(Base, () => SomeLongRunningOperation(Base.Document.Current));
    return list;
}

Besides that, in the end of your function SomeLongRunningOperation you may consider adding code like this:

ARPayment arPayment = cuArPayment;
var newGraph = PXGraph.CreateInstance<ARPaymentEntry>();
newGraph.Document.Current =
    Base.Document.Search<ARPayment.refNbr>(arPayment.RefNbr, arPayment.DocType);
 
//  Some additional lines of code
newGraph.Persist();
PXRedirectHelper.TryRedirect(newGraphPXRedirectHelper.WindowMode.Same);

In that case after SomeLongRunningOperation function will finish it's processing, it will refresh current screen of user.

Summary

If you deal with PXLongRunningOperation, you'll need to have following elements:

  1. Save existing item
  2. Return list with one element ( current primary element of view ), which will leave user on the same element
  3. In the end of PXLongRunningOperation call TryRedirect which will refresh currently selected view

 

 
Comments are closed