Wednesday, August 13, 2025

Custom Sales Line Discount Calculation (Similar to Poland Localization) in D365 F&O X++



I recently had a requirement to calculate the Sales Line discount price in the same way as it is done for Poland localization.

If you’re not familiar with it, you can read Microsoft’s official documentation here: 

'Line discount calculation from the unit price for Poland - Finance | Dynamics 365 | Microsoft Learn'.

Technical Approach:

As per standard, the PriceDisc class, specifically the price2Amount() method, is responsible for calculating price amounts for Sales Orders, Purchase Orders, and Sales Quotations.

For Poland, the price2Amount() method is used to handle discount calculation.

Our goal is to reuse the same Poland logic for our custom requirement.

Steps to Implement:

  1. Create an Extension for the PriceDisc class.

  2. Write a Chain of Command (CoC) for the price2Amount() method.

  3. In the post-method CoC, duplicate the Poland logic and override the amount value.


Context Framework Usage

I used the disposable context framework to ensure:

  • The customization triggers only for Sales.

  • Added custom flag “Custom Sales Price Calculation” in AR parameter form/Prices tab to acts as an extra safeguard. You can turn it on only for the specific company where you want this customization to run, and keep it off for others. It’s also useful for quickly disabling the customization if something isn’t working as expected.


  • Create the context class with necessary variables and methods.

Plese refer this blog if you are not familiar with Context framework '(3) D365F&O. The magic of the Grabage Collection (GC) or story about Disposable Context | LinkedIn'

To initialize the context only for Sales:

  • Extend the SalesPurchLinePriceDiscCalculation class.

  • Override the calcPrice2LineAmount() method.

  • Initialize the context there and set the class variable as true, so the customization runs only for Sales Orders.

This is important because PriceDisc is used for both Sales and Purchase scenarios, and we don’t want

the customization to affect Purchases.

 

Testing Scenarios

Make sure you test both unit and functional cases to confirm the customization works and doesn’t impact other processes:

  1. Intercompany scenario

  2. Sales Return

  3. Sales Margin functionality

  4. Ensure Sales Quotations and Purchase Orders are not affected

Note: I have not tested this with Retail Sales Orders. Please validate this scenario if relevant to your environment.

Disclaimer: Please note that these suggestions are based on my personal experience and may not be the only solution. As always, thoroughly test any changes in your test environment before moving them to Production.

Custom Sales Line Discount Calculation (Similar to Poland Localization) in D365 F&O X++

I recently had a requirement to calculate the Sales Line discount price in the same way as it is done for Poland localization . If you’re n...