Solved

The current company is lost when I tried to save inside an asynchrony method

  • 14 August 2023
  • 5 replies
  • 58 views

I'm having trouble with this way of saving the data in Acumatica inside an async method for some reason when I call the method and try to save the current data it always takes the last company in the database. I mean loses the current context.

I have a site in 22R2 with 2 company:

 

Database:

 

I have this code:

 

Asynchrony method

   public async Task SaveAsync(DeviceLicenseData? data)

        {

       

 

            if (data == null || string.IsNullOrEmpty(data.AccessToken))

            {

                await ResetAsync();

                return;

            }

 

            SaveToAcumatica(data);

        }

 

 

Method to save in Database

  public void SaveToAcumatica (DeviceLicenseData data)

        {

 

            License.Current = License.Search<License.fRServerId>(data.DeviceId);

            License.Cache.AllowUpdate = true;

 

            var frlicense = License.Current ?? new License();

 

 

            frlicense.FRServerId = data?.DeviceId ?? default;

            frlicense.FRHubServerName = data?.ServerName ?? string.Empty;

 

            if (data.RenewalDate != null)

                frlicense.FRLicenseRenavalDate = Convert.ToDateTime(data.RenewalDate);

            frlicense.FRHubServerURL = Convert.ToString(data.HubUrl);

            frlicense.UsrFRTokenExpiry = Convert.ToDateTime(data.HardTokenExpiry);

 

            frlicense.Features = data?.Features != null

             ? string.Join(" ", data.Features)

             : string.Empty;

 

            frlicense.Status = "Active";

 

           

 

            Actions.PressSave();

 

        }

 

 

When I review the current company always have 3 don’t matter that I position in company 2.

PXInstanceHelper.CurrentCompany; = 3

 

 

My question is how I can return to the current company inside the asynchrony method?

icon

Best answer by jesusquintal45 14 August 2023, 23:18

View original

5 replies

Well, I found this way to fix the problem:

 

    using (var scope = new PXLoginScope("admin@Company"))

                {

///Code

}

Userlevel 7
Badge

Thank you for sharing your solution with the community @jesusquintal45!

Userlevel 7
Badge +5

@jesusquintal45

I just want to say that I would not recommend using async this way in application code. 

The framework is not designed and not tested for such usage.

 

@jesusquintal45

I just want to say that I would not recommend using async this way in application code. 

The framework is not designed and not tested for such usage.

 

Thanks for your recomendation. What option can I use for this? In this case, I integrated my own dll that used the asynchronous method and I found no other option to be able to call them than with Task.execute as follows:

 

[InjectDependency]
          public IDeviceLicenseFactory _Factory { get; place; }

 

 

public PXAction<FRLicense> refreshToken;

    [PXButton(CommitChanges = true)]
          [PXUIField(DisplayName = "Refresh Token", MapEnableRights = PXCacheRights.Select)]
          Protected virtual IEnumerable RefreshToken (PXAdapter)
          {

              FRLicenseServiceXMS apiServicesLicense = new FRLicenseServiceXMS();

              var task = Task. execute(async() =>
                            expect apiServicesLicense.RefreshLicense(_Factory));
              task.Wait();

              if (!task.Result)
              {

                  throws a new exception PXSetPropertyException(FRMessagesLoyalty.RefreshExceptionMessage, PXErrorLevel.Warning);
              }
              License.View.RequestRefresh();
              this.Cancel.Press();

              return adapter.Get();
          }

       public async Task<bool> RefreshLicense(IDeviceLicenseFactory _Factory)
        {
       
                var managerLicence = await _Factory.GetManagerAsync();
                return await managerLicence.RefreshAsync(false);
 

        }

 

Userlevel 7
Badge +5

@jesusquintal45  well, I think I’d just use something like 

 public void Save(DeviceLicenseData? data)
{
            if (data == null || string.IsNullOrEmpty(data.AccessToken))
            {
                ResetAsync().Wait();
                return;
            }
            SaveToAcumatica(data);
}

 

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved