Skip to main content
Solved

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


jesusquintal45
Freshman II

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?

Best answer by jesusquintal45

Well, I found this way to fix the problem:

 

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

                {

///Code

}

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

5 replies

jesusquintal45
Freshman II
  • Author
  • Freshman II
  • 2 replies
  • Answer
  • August 14, 2023

Well, I found this way to fix the problem:

 

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

                {

///Code

}


Chris Hackett
Community Manager
Forum|alt.badge.img
  • Acumatica Community Manager
  • 2781 replies
  • August 14, 2023

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


Dmitrii Naumov
Acumatica Moderator
Forum|alt.badge.img+7
  • Acumatica Moderator
  • 648 replies
  • August 15, 2023

@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
Freshman II
Dmitrii Naumov wrote:

@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);
 

        }

 


Dmitrii Naumov
Acumatica Moderator
Forum|alt.badge.img+7
  • Acumatica Moderator
  • 648 replies
  • August 15, 2023

@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


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