Hello
I am customizing the Pay Bills screen and overriding the apdocumentlist delegate.
My requirement is:
- When a specific Payment Method is selected
- Only show AP Bills whose corresponding invoice in an external system has status Pending
- The external invoice ID is already stored on
APInvoicein a custom field such asUsrExternalInvoiceId - I cannot store the external invoice status on
APInvoiceor in another database table
My current view override is similar to:
public delegate IEnumerable APDocumentListDelegate();
[PXOverride]
public IEnumerable apdocumentlist(APDocumentListDelegate baseMethod)
{
IEnumerable baseResult = baseMethod();
PayBillsFilter filter = Base.Filter.Current;
if (filter == null ||
!string.Equals(
filter.PayTypeID,
ExternalPaymentMethodID,
StringComparison.OrdinalIgnoreCase))
{
return baseResult;
}
var filtered = new PXDelegateResult();
foreach (object row in baseResult)
{
if (invoice == null)
continue;
APInvoiceExt ext =
PXCache<APInvoice>.GetExtension<APInvoiceExt>(invoice);
string externalInvoiceId =
ext?.UsrExternalInvoiceId?.Trim();
if (string.IsNullOrEmpty(externalInvoiceId))
continue;
// Need to retrieve the current invoice status from the external system here.
// Only add the AP Bill if the external status is "Pending".
}
return filtered;
}The external API method is asynchronous:
string response = await service.SearchInvoices(
payerId,
invoiceNumber: externalInvoiceId);I do not want to use:
.GetAwaiter().GetResult()or:
Task.Run(...)to block the request.
Normally, for external API calls in Acumatica, I would use:
Base.LongOperationManager.StartAsyncOperation(
cancellationToken =>
SomeAsyncMethod(cancellationToken));However, apdocumentlist is a synchronous IEnumerable delegate and needs the external API result immediately in order to decide whether each AP Bill should be returned.
Also, starting a long operation directly from a data view delegate does not appear to be the correct pattern as I understand.
Question
What is the recommended pattern approach for this scenario?
Specifically:
How should an asynchronous external API call be used when the result is required to filter a synchronous data view such as apdocumentlist?
The goal is simply:
APInvoice.UsrExternalInvoiceId
↓
Call External API
↓
Get current invoiceStatus
↓
If status == "Pending"
↓
Show bill on the gridCould you please advise on the correct approach and share an example if possible? Please also correct me if my understanding is incorrect.