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?