Async Delegate Doesn't Save Order in Any Event  Topic is solved

Discussions relating to plugin development, and the Jiwa API.

Re: Async Delegate Doesn't Save Order in Any Event

Postby Mike.Sheen » Fri Aug 25, 2023 2:07 pm

SBarnes wrote:Couldn't that cause a concurrency exception, if someone then edited the sales order?


Yes, but there is only so much we can do.

You could catch the concurrency exception and in the catch re-read the sales order then, set the custom field value (not call external API again - you'd already have the value you needed from it) and then save.

That would be good enough.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Async Delegate Doesn't Save Order in Any Event  Topic is solved

Postby Mike.Sheen » Fri Aug 25, 2023 2:13 pm

So, the revised guidance is:

Code: Select all
salesOrder.ProcessingEnd += async delegate (object sender, EventArgs e)
{
   // create new sales order object   
   SalesOrder tempOrder = Manager.BusinessLogicFactory.CreateBusinessLogic<SalesOrder>(null);
   // read order (use salesOrder.RecID)
   tempOrder .Read(salesOrder.RecID);
   // Make external API call
   var orderID = ExternalAPI();
   // update custom field (on the sales order instance we created and read in here)
   tempOrder.CustomFielValues.SetValue<string>("OrderID", orderID);
   try
   {
      // Save (not the sales order outside the delegate, the one we created in here)
     tempOrder.Save();
   }
   catch (ConcurrencyConflictException)
   {
     // re-read and try saving again
     tempOrder .Read(salesOrder.RecID);
     tempOrder.CustomFielValues.SetValue<string>("OrderID", orderID);
     tempOrder.Save();
   }
}
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Previous

Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 36 guests