Skip to main content

Notifications

Announcements

No record found.

How to extend Purchase/Sales Line Type in Business Central

This blog may seem very narrow-focused, but here we will use tricks that could be widely used.

Assume you want to add a new option to the Sales or Purchase Line Type. This approach will work the same for both, so let's say Purchase Line Type. 

The story

You are building the Fishing app. You created a new table - Fish, and want to add some logic when you catch a fish. To do so, you decide to add a new option to the Purchase Line Type. 

Standard way 

The standard list of purchase line types consist of

0576.pastedimage1599545192705v3.png

I guess at this point you ask yourself, what could be complicated here? Just create an Enum extension and that's it. Well, let's give a try.

3201.pastedimage1599545216987v4.png

Publishing the app, going into the purchase order and...

1018.pastedimage1599545802244v5.png

... nothing. Hm. Well, if this was so easy I will not decide to write this blog, right?

Digging deeper

Let's have a closer look at how Type drill-down is organised. The code is hidden in the "Purchase Order Subform" page.

If your Application mode is Advanced - you can close this blog at this point.

Just because in the Advanced Application mode - the drill-down type screen will have your Fish option, coming from the enum Extention automatically.

4188.pastedimage1599542066817v1.png 

But, if Application mode is Basic or Suite, then what you see is not a standard options-choice screen, it's a separate page.

7268.pastedimage1599546123623v6.png

Show your option

As it's a page with a separate "Option Lookup Buffer" table, to show your option we need to add a new record. 

But before let's investigate how and when this buffer table is filling with the standard values. The answer - when you open purchase document

0878.pastedimage1599546498251v8.png

and it's our chance - event "OnBeforeIncludeOption"

7776.pastedimage1599546783849v9.png

Actually, if you look closer, you will see that this function IncludeOption - does not add an option to the table as a record, but only tells the system if this option should be added.

Let's subscribe and tell the system that - yes, we want our new option to be added to the table.

6138.pastedimage1599547167435v10.png

The trick here is that at the input we get the current OptionIndex, starting from 0. And we need to check if the current option is our Fish.

So, our Fish option will have an OptionIndex = 6 (0-5 standard values, and 1 extension value).

To check if the current OptionIndex is our fish we need to calculate the Index of our Fish value. 

PurchLine.Type.Ordinals- will give a list of option values. The list in AL starts from 1, not from 0.

PurchLine.Type.Ordinals.IndexOf - will give us the index of the option in a list.

PurchLine.Type.Ordinals.IndexOf(PurchLine.Type::"AIR Fish".AsInteger()) - will give us the index of the option in a list, where option value is Fish. AsInteger will give us the id of the option. 

There is a good blog describing these definitions https://www.kauffmann.nl/2020/07/16/converting-enum-values-in-al/. Also at the end of current blog, there will be a link to my youtube video, describing these things.

PurchLine.Type.Ordinals.IndexOf(PurchLine.Type::"AIR Fish".AsInteger()) - 1 : will convert the index from the list to the OptionIndex.

Let's publish and have a look at the result

4846.pastedimage1599557257767v11.png

Great, now our new value is in the list. But why it's without a name, just an index?

The reason is that FieldRef tries to get value from the Index. And Index, in this case, should match Enum Id. 

2210.pastedimage1599557519269v12.png

In our case, the OptionIndex is 6, but the custom Enum Value has the Id of 50100 (the id we assigned in enumextension). 

And when in the next function CreateNew, the logic executes FormatOption - it just cannot find a name for the 6 enum value, because it just does not exist.

4645.pastedimage1599557710308v13.png

Well, I thought that it's the end, but I got an idea.

Have a look, that's the standard code

6281.pastedimage1599558095374v15.png

and here is mine

0726.pastedimage1599557882866v14.png

When standard insert an option value to the drill-down table, I discover MY option value there by the index and replace it with the real Id and Name.

Publish, and...

5707.pastedimage1599558184325v16.png

From now you can extend other logic, based on the new Purchase Line Type

Code

The code is available here https://github.com/dkatson/Blog-How-to-extend-Purchase-Sales-Line-Type-in-Business-Central

Enjoy!

More on the YouTube

Comments

*This post is locked for comments