Skip to main content

Creating a Business Date context scope

  • January 22, 2025
  • 1 reply
  • 34 views

darylbowman
Captain II
Forum|alt.badge.img+13

At some point, you may find it necessary (like I did) to change the Acumatica Business Date for a process.

In my case, I was modifying an ISV application which used Business Date in the processing of many documents within two different actions. Because the code was written in a relatively closed fashion, with key methods being private, it was much easier to change the Business Date within the context of the actions than to try to change how the date parameter was being assigned.

I decided to take a page out of Acumatica’s playbook and implement my own scope. (You can read more about Acumatica context scopes here.)

The PXScreenIDScope was very nearly exactly what I needed, except for changing ScreenID instead of BusinessDate.

After my modifications, here is the result:

using System;
using System.Web;
using PX.Common;
using PX.Data;

namespace MyCustomScope
{
    public class DXBusinessDateScope : IDisposable
    {
        private readonly DateTime? _businessDate;

        private readonly DateTime? _oldBusinessDate;

        private readonly PXGraph _graph;

        public DXBusinessDateScope(DateTime? businessDate)
        {
            _businessDate = businessDate;
            _oldBusinessDate = PXContext.GetBusinessDate();
            PXContext.SetBusinessDate(businessDate);
        }

        public DXBusinessDateScope(PXGraph graph, DateTime? businessDate)
            : this(businessDate)
        {
            _graph = graph;
            graph.Accessinfo.BusinessDate = businessDate;
        }

        public void Dispose()
        {
            PXContext.SetBusinessDate(_oldBusinessDate);
            if (_graph != null)
                _graph.Accessinfo.BusinessDate = _oldBusinessDate;
        }
        /*
        public void AddToContext(HttpContext context)
        {
            PXContext.SetBusinessDate(context, _businessDate);
        }*/
    }
}

Then, I used the scope like this:

DateTime? businessDate = *assign date*;

// Changes the Business Date in the context of the scope
using (new DXBusinessDateScope(businessDate))
{
    // Execute code with a different Business Date
    return baseMethod(adapter);
}

The Business Date is changed within the context of anything happening inside the scope, including graph initialization. When the scope is disposed, the Business Date is reverted.

Hopefully this will help someone in a similar situation.

Did this topic help you find an answer to your question?

1 reply

Chris Hackett
Community Manager
Forum|alt.badge.img
  • Acumatica Community Manager
  • 2756 replies
  • January 23, 2025

Thank you for sharing this tip with the community ​@darylbowman!


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