Skip to main content
Answer

Unable to Update value in the PXLongRun Operation

  • March 22, 2023
  • 8 replies
  • 285 views

Forum|alt.badge.img

Hello Everyone,

In the persist delegate, I would like the custom field inside the PXLong Run operation but value is not updating. Can anyone help me on this?

If try to save again, it is re-calling the Persist method and going to the infinite loop.

Here is the sample code for your reference.

 

      public delegate void PersistDelegate();
[PXOverride]
public void Persist(PersistDelegate baseMethod)
{
baseMethod();
if (Base.Document.Current != null)
{
PXLongOperation.StartOperation(Base, delegate ()
{
try
{
//my custom logic is here

//Updating the custom field value below, value is holding in the caches but updating

Base.Document.Cache.SetValue<SOOrderExt.usrCustomField>(Base.Document.Current, true);
Base.Document.Cache.Update(Base.Document.Current);
}
catch (Exception ex)
{
throw ex;
}
}

}

}

 

Best answer by Naveen Boga

Hi @nsmith51  If you write such updates it won’t persist, once LongRun is completed then the caches will be cleared in this.

You can create a new graph instance, and fill in the current details into the new graph and do updates with this graph, and save.

8 replies

aaghaei
Captain II
Forum|alt.badge.img+10
  • Captain II
  • March 22, 2023

Can you elaborate why are you running LongOpr in Persist?

can you provide a background and the use case you are trying to achieve apparently in the Sales Order entry?

I don’t recall if I have ever seen the LongOpr timer appears on any screen when we click on Save.


Forum|alt.badge.img
  • Author
  • Jr Varsity III
  • March 22, 2023

Hi @aaghaei  Thanks for your response.

We are using LongRun operation because, we are creating the some transactions like (IN Receipts), hence it is suggestable to use the LongRun to run on a separate thread.

It is very common in Sales Order screen to use the LongRun when we have some custom/API related logic, Have you never worked any Tax plug-ins, you can see the long run in the SO.

 

 


aaghaei
Captain II
Forum|alt.badge.img+10
  • Captain II
  • March 22, 2023

I have not worked with tax plug-ins so cannot be of help on this. Sorry.

I have worked plenty with the PXLongOperation but have not seen it in the Persist which is invoked when Save is pressed.

On a side note, I might be missing something but just looked into the SOOrderEntry graph and all its extensions but couldn’t find any PXLongOperation in Persist or its Delegate.


Forum|alt.badge.img+7
  • Captain II
  • March 23, 2023

You might then consider calling PXDatabase.Update to update your custom field to avoid calling Persist the second time.


Vinay Koppula
Semi-Pro II
Forum|alt.badge.img+1

@nsmith51 Have you tried placing the basemethod() after the LongOperation process?


Forum|alt.badge.img+9
  • Semi-Pro III
  • March 27, 2023

Hi @nsmith51 ,

Can you please try below code snippet to update your custom field.

I am not verified this code, but it will helps to implement your logic for long operation.

public delegate void PersistDelegate();
    [PXOverride]
    public virtual void Persist(PersistDelegate baseMethod)
    {
        baseMethod();

        if (Base.Document.Current != null)
        {
            PXLongOperation.StartOperation(Base, delegate
            {
               CustomMethod();
            });
        }
    }

public void CustomMethod()
    {
try
{

      //your logic is here to update the custom field

    catch (Exception e)
        {
            throw new PXOperationCompletedWithErrorException(e.Message);
        }

        graph.Update();
        graph.Persist();
}

Hope, this helps!

Regards,

Sweta


Naveen Boga
Captain II
Forum|alt.badge.img+19
  • Captain II
  • Answer
  • March 27, 2023

Hi @nsmith51  If you write such updates it won’t persist, once LongRun is completed then the caches will be cleared in this.

You can create a new graph instance, and fill in the current details into the new graph and do updates with this graph, and save.


Forum|alt.badge.img
  • Author
  • Jr Varsity III
  • March 27, 2023

Understood. Thanks for the help @Vinay Koppula @Naveen Boga @Django @aaghaei @sweta68