Dynamics 365 Business Central: How to use standard customerPayment API to get customer payments (customerPayments/customerPaymentsJournals APIs returns empty records)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to use standard customerPayment API to get customer payments in Business Central.
I’ve seen questions about the standard customerPayment API usage in Dynamics 365 Community twice in a row recently, so in this post I want to discuss it briefly. More details: Can’t able to Get Customer Payment through API v2.0 (dynamics.com) and CustomerPayment/CustomerPaymentJournals API returns empty records. (dynamics.com).

First of all, the customerPayment API is a standard API.
customerPayment resource type: Represents a customer payment in Business Central.
For example,
Get customerPayments: Retrieve the properties and relationships of a customer payment object for Business Central.

GET businesscentralPrefix/companies({id})/customerPaymentsJournals({id})/customerPayments({id})
GET businesscentralPrefix/companies({id})/customerPayments({id})

PS:
1. Standard API for Dynamics 365 Business Central SaaS (Get, Post, Patch, Delete and Basic query parameters – filter, orderby, select…)
2. Using OAuth to connect Business Central APIs and Web Service in Postman

The biggest problem when using this standard API is that users cannot get data. For example, on the Cash Receipt Journals page, I created two lines of data under the General batch.

The data (General Journal Batches) can be obtained from the customerPaymentJournals API.

https://api.businesscentral.dynamics.com/v2.0/c8558e71-6ad8-4fcd-9f29-3ca165b41bbe/Sandbox241/api/v2.0/companies(938320db-e10b-ef11-9f8d-6045bdacaf36)/customerPaymentJournals

However, the two HTTP requests mentioned in MS Learn (Docs) above cannot obtain data. customerPayments APIs returns empty records.

https://api.businesscentral.dynamics.com/v2.0/c8558e71-6ad8-4fcd-9f29-3ca165b41bbe/Sandbox241/api/v2.0/companies(938320db-e10b-ef11-9f8d-6045bdacaf36)/customerPayments
https://api.businesscentral.dynamics.com/v2.0/c8558e71-6ad8-4fcd-9f29-3ca165b41bbe/Sandbox241/api/v2.0/companies(938320db-e10b-ef11-9f8d-6045bdacaf36)/customerPaymentJournals(587cf070-a414-ef11-9f88-000d3a50c332)/customerPayments

The same applies when using the $expand method from customerPaymentJournals API.

https://api.businesscentral.dynamics.com/v2.0/c8558e71-6ad8-4fcd-9f29-3ca165b41bbe/Sandbox241/api/v2.0/companies(938320db-e10b-ef11-9f8d-6045bdacaf36)/customerPaymentJournals?$expand=customerPayments

PS: Dynamics 365 Business Central: How to use filter expressions in API related resource ($expand)

Why? The answer can be found in the standard code.
page 30055 “APIV2 – Customer Payments” -> trigger OnNewRecord(BelowxRec: Boolean)

Yes, because the SourceTable of this standard API is “Gen. Journal Line”, Microsoft adds a default filter, and only detail lines with “Document Type”=Payment, “Account Type”=Customer and “Applies-to Doc. Type”=Invoice will be displayed.

At this time, the data can be obtained normally through customerPaymentJournals?$expand=customerPayments and customerPaymentsJournals({id})/customerPayments({id}).

https://api.businesscentral.dynamics.com/v2.0/c8558e71-6ad8-4fcd-9f29-3ca165b41bbe/Sandbox241/api/v2.0/companies(938320db-e10b-ef11-9f8d-6045bdacaf36)/customerPaymentJournals?$expand=customerPayments
https://api.businesscentral.dynamics.com/v2.0/c8558e71-6ad8-4fcd-9f29-3ca165b41bbe/Sandbox241/api/v2.0/companies(938320db-e10b-ef11-9f8d-6045bdacaf36)/customerPaymentJournals(587cf070-a414-ef11-9f88-000d3a50c332)/customerPayments

But if you only use the customerPayments API, another error will occur.

https://api.businesscentral.dynamics.com/v2.0/c8558e71-6ad8-4fcd-9f29-3ca165b41bbe/Sandbox241/api/v2.0/companies(938320db-e10b-ef11-9f8d-6045bdacaf36)/customerPayments

You must specify a journal batch ID or a journal ID to get a journal line.

This is also defined in the code.

So we need to add a journal batch ID or a journal ID.

https://api.businesscentral.dynamics.com/v2.0/c8558e71-6ad8-4fcd-9f29-3ca165b41bbe/Sandbox241/api/v2.0/companies(938320db-e10b-ef11-9f8d-6045bdacaf36)/customerPayments?$filter=journalId eq 587cf070-a414-ef11-9f88-000d3a50c332

Or

https://api.businesscentral.dynamics.com/v2.0/c8558e71-6ad8-4fcd-9f29-3ca165b41bbe/Sandbox241/api/v2.0/companies(938320db-e10b-ef11-9f8d-6045bdacaf36)/customerPayments?$filter=id eq f7afd139-4621-ef11-9f88-00224868ce08

That’s all. If you encounter similar problems in the future, you can investigate the standard code first, just like we did this time. More details: Dynamics 365 Business Central: Can we extend standard APIs? – Download Standard Source Code for APIs
And I sincerely hope that this information can be added to MS Learn (Docs).

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL