-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARSetupMaint.cs
More file actions
58 lines (49 loc) · 2.26 KB
/
ARSetupMaint.cs
File metadata and controls
58 lines (49 loc) · 2.26 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
using PX.Data;
using System.Collections;
using System.Net.Http;
using System.Threading;
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;
using (var client = HttpClientFactory.CreateClient())
{
// this is a special way to run async operations in Acumatica
responseBody = Base.LongOperationManager.Await(async (CancellationToken token) =>
{
HttpResponseMessage response = await client.GetAsync("https://reqres.in/api/users", token);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
});
}
// 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();
}
}
}