-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARSetupMaint.cs
More file actions
71 lines (60 loc) · 2.95 KB
/
ARSetupMaint.cs
File metadata and controls
71 lines (60 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections;
using System.Net.Http;
using PX.Async;
using PX.Data;
namespace GetValueFromAPIExample
{
public class ARSetupMaint_Extension : PXGraphExtension<PX.Objects.AR.ARSetupMaint>
{
// we need IsActive here to be able to enable/disable the extension conditionally.
// for more information see https://help.acumatica.com/Help?ScreenId=ShowWiki&pageid=cd70b408-b389-4bd8-8502-3d9c12b11112
public static bool IsActive()
{
return true;
}
[InjectDependency]
// IHttpClientFactory is used to create HttpClient instances efficiently.
// It helps manage the lifetime of HttpClient, preventing common issues like
// socket exhaustion caused by instantiating HttpClient manually.
public IHttpClientFactory HttpClientFactory
{
get;
set;
}
// this adds a button to the ARSetup screen
public PXAction<PX.Objects.AR.ARSetup> GetDataFromExternalAPI;
[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "Get Data From External API")]
protected IEnumerable getDataFromExternalAPI(PXAdapter adapter)
{
string responseBody = "";
var key = Guid.NewGuid();
// this is a special way to run async operations in Acumatica
Base.LongOperationManager.StartAsyncOperation(key, async cancellationToken =>
{
using (var client = HttpClientFactory.CreateClient())
{
HttpResponseMessage response = await client.GetAsync("https://reqres.in/api/users", cancellationToken);
response.EnsureSuccessStatusCode();
responseBody = await response.Content.ReadAsStringAsync();
}
}
);
// wait for the operation to complete using the key we've assigned to the operation
Base.LongOperationManager.WaitCompletion(key);
//check if the operation has completed successfully. Throw an exception if it did not
var details = Base.LongOperationManager.GetOperationDetails(key);
if (details.Status == PXLongRunStatus.Aborted)
{
throw details.Message != null ? details.Message : new PXException("The operation was aborted unexpectedly.");
}
// since the custom field we want to write the data to is defined in an extension, we need to get the extension object first
var extension = PXCache<PX.Objects.AR.ARSetup>.GetExtension<ARSetupExt>(Base.ARSetupRecord.Current);
extension.UsrTestField = responseBody;
//need to update the record for the changes to be properly applied
Base.ARSetupRecord.Update(Base.ARSetupRecord.Current);
return adapter.Get();
}
}
}