Skip to main content

🔄 Convert Available Inventory to Kilograms (KG) in X++ – D365 F&O

When working with raw materials in Dynamics 365 Finance & Operations (D365 F&O), it’s common to encounter situations where the inventory is stored in one unit (like Liters or Packs), but needs to be viewed or calculated in Kilograms (KG) for costing, reporting, or analysis. In this blog, we’ll walk through a practical X++ code snippet that demonstrates how to convert available inventory (AvailPhysical) into KG using D365’s standard Unit of Measure (UOM) conversion. 📌 Business Scenario Suppose you have a chemical item stored in Liters , but the production or costing team needs to see its available inventory in KG . You’ll need to: Fetch the item’s AvailPhysical from the InventSum table. Get the current unit of measure (UOM) for the item. Convert that quantity into KG , based on predefined unit conversion. 💻 X++ Code – Inventory Conversion to KG Here’s the code block that does exactly that: //******************** Avail Physical as per KG Unit conversion *...

How to Refresh a Form or Data Source in D365FO Using X++

 

Introduction

In Microsoft Dynamics 365 Finance & Operations (D365FO), refreshing the form after an action (like inserting, updating, or deleting a record) is essential for keeping the UI updated with the latest data. In this blog, we’ll explore two ways to refresh the form in X++:

Refreshing the entire form using taskRefresh
Refreshing a specific data source using research

Let's dive into the best practices for implementing these refresh methods!


🔄 Refreshing the Entire Form

If you need to refresh the whole form, use the taskRefresh method. This method is useful when multiple data sources are involved, and you want to reload everything.

📌 X++ Code for Full Form Refresh

public void refreshForm()
{
    // Get the current form instance
    FormRun formRun = this.formRun();

    // Check if formRun is valid before refreshing
    if (formRun)
    {
        info("Refreshing the form...");
        formRun.task(#taskRefresh); // This triggers a full UI refresh
    }
    else
    {
        warning("FormRun is NULL. Refresh skipped.");
    }
}


🔍 How It Works

  1. Gets the form instance using this.formRun().
  2. Checks if formRun is not null (to avoid errors).
  3. Calls formRun.task(#taskRefresh) to refresh the entire UI.
  4. Displays an info message when the refresh happens.

💡 When to Use?

  • After inserting or updating records that affect multiple data sources.
  • When you want to ensure all changes are reflected in the UI.

🔄 Refreshing a Specific Data Source

Instead of refreshing the entire form, sometimes you just need to refresh a specific data source. This can improve performance by reloading only the required data.

📌 X++ Code for Data Source Refresh


public void refreshDataSource()
{
   Table_ds.research(); // Refreshes only this data source
}

🔍 How It Works

  1. Calls research() on the specific data source to reload data.
  2. Does not affect other data sources in the form.

💡 When to Use?

  • When only a single table is updated, and you don’t need to refresh the full form.
  • To improve performance by avoiding unnecessary UI reloads.

🔄 Bonus: Ensuring Data Accuracy with reread()

If you're updating records but haven’t committed the changes to the database yet, you should use reread() before research().


public void refreshDataSource()

{

    Table_ds.reread(); // Reloads the latest uncommitted data

    Table_ds.research(); // Refreshes the UI

}


📝 Conclusion

Refreshing forms in D365FO is crucial for ensuring a smooth user experience. Depending on your needs, you can:
✅ Use taskRefresh to reload the entire form.
✅ Use research to refresh a single data source.
✅ Combine reread() + research() to handle unsaved changes.

By implementing these best practices, you’ll improve form responsiveness and data accuracy in your D365FO applications! 🚀



Comments

Popular posts from this blog

How to Open a Form with Filtered Data Using a Button in X++ – A Step-by-Step Guide

 In Dynamics 365 for Finance and Operations (D365FO), a common requirement is to open a form dynamically from another form and pass filtered data based on a specific condition. This functionality can enhance user experience by allowing them to interact with multiple forms seamlessly, while keeping the data relevant and focused. In this blog, we’ll explore how to implement such a solution using X++, where a user clicks a button on Form 1 (such as a list of sales orders), and based on a selected record, Form 2 (such as invoice details) opens with only the relevant filtered data. Scenario Overview Let’s assume the following scenario: Form 1 : Displays a list of sales orders, and each order has an OrderID , CustomerID , and OrderAmount . Form 2 : Displays details of invoices (from the InvoiceDetails table) that are linked to the selected OrderID from Form 1 . The goal is to click a button on Form 1 , pass the OrderID to Form 2 , and display only the relevant invoice records relate...

Sorting Data in X++ (D365FO) Grids Using Form Data Source Events

  Introduction : In Dynamics 365 Finance and Operations, form grids often display data retrieved from a table or query. However, the default sorting applied may not always align with business requirements. Customizing sorting behavior at runtime ensures that the grid data is presented in a meaningful order for users. This blog post demonstrates how to use the Initialized event of a form data source to apply custom sorting to a grid. We will sort the grid rows based on a specific field ( DisplayOrder ) in ascending order. Understanding the Code : Here’s the complete code snippet: --------------------------------------------------------------------------------------------------------------- [FormDataSourceEventHandler(formDataSourceStr(MyFormDataSource,  MyTable), FormDataSourceEventType::Initialized)] public static void MyFormDataSource_OnInitialized(FormDataSource sender, FormDataSourceEventArgs e) {     // It will clear if any other sorting is applied on the grid ...