Business central API: How to get the general ledger entries of a posted sale invoice ?

Hello,

How can we get the general ledger entries of a posted sale invoice via the API of business central ?

We tried with the following request

…/api.businesscentral.dynamics.com/v1.0/api/beta/companies(id)/generalLedgerEntries?$top=100&$filter=(documentNumber eq ‘1910022’) and (documentType eq ‘Invoice’)

but FILTER on documentNumber is not 100% correct because i assume documentNumber 1910022 could concern also a posted purchase invoice or even other thing.

Thank in advance for your help.

Regards.

Guy

Hi,

You can, for example, refer to the Navigate function to see how this kind of filtering is handled in standard NAV / BC. If you open a posted invoice click Navigate and drill down to G/L entries, you can see that there are two filter applied on the record: Document No. and Posting Date. In fact, these are the only two parameters that are passed to the Navigate page. Usual practice assumes that numbers to posted documents should be assigned in a way that rules out intersections. Different document types should always have different numbers.

But, if you have doubts in the correctness of filtering, you can set additional filter on the “Source Code” field - for sales it will be ‘SALES’.

Thank you for your help,

In the API documentation i don’t see the field “Source code” , did i miss something ?

You are right by saying that “Different document types should always have different numbers” but i made a test and Business Central allow intersections in the numbering so filtering on document number is not 100% correct. For example when you have a posted sale invoice with the same number as a posted purchase invoice and you open the posted sale invoice and click Navigate you receive the message “This combination of document number and posting date has been used more than once” and when you drill down to G/L entries you see the G/L entries of the 2 invoices (see screenshots below).

GLSaleInvoice1.jpg

You are right, that’s my mistake - I was sure that source code is published in the standard API for G/L entries.

Then there is no way to refine filtering except publishing your own API, as, for example, Dmitry Katson explains in his blog post: https://community.dynamics.com/nav/b/katsonsnavblog/archive/2018/10/10/how-to-create-custom-apis-for-business-central-even-from-macos

In a custom API, you can show all the field you need.

As for document numbers, you’re absolutely correct - recommendation to configure number series in a way to prevent accidental overlapping is no more than a recommendation or a best practice. This is not a requirement imposed by the system core, and your example perfectly illustrates it.

So, your solution would be to publish a custom API or a web service and appy an additional filter on the sorce code. But even that is not 100% reliable, because source code setup can change in time, and old entries posted with an obsolete source code will be lost in that case. Still the best way is try to prevent posting documents of different types with the same number.

Thank you for your help, good to know it’s possible to customize the API :wink:

Please verify the answer that helped you. Thank you.

Hello Erik

Yes the answer helped us in the sense that we are going to make sure that all the different documents will not have the same number.

Regards.