Thursday, December 18, 2025

Handling Temporary Files and Browser Downloads in D365FO

Deep Dive: Handling Temporary Files and Browser Downloads in D365FO

In the transition from Dynamics AX 2012 to Dynamics 365 Finance & Operations, file handling has undergone a paradigm shift. Because the application is cloud-hosted, developers can no longer rely on local file system paths like C:\Temp.

Instead, professional X++ development requires understanding System.IO.Streams and Azure Blob Storage interaction. This article explores how to generate files efficiently, manage memory, and force specific browser behaviors.

1. The Architecture: Where does the file go?

When you generate a file in X++, it exists initially in the AOS server's memory. To get it to the user's browser, the system follows this sequence:

  1. Stream Generation: Data is written to a .NET MemoryStream.
  2. Azure Upload: The system uploads this stream to a temporary Azure Blob Storage container via the File::SendFileToTempStore method.
  3. SAS Token: A secure URL (with a Shared Access Signature) is generated.
  4. Navigation: The browser is directed to this URL to initiate the download.
Pro Tip: Unlike legacy AX, the file never touches the local disk of the server. It moves directly from Memory (RAM) to the Cloud Storage.

2. The Professional Extension: Force "New Tab" Download

The standard File::SendStringAsFileToUser is useful for quick debugging, but for a polished user experience, we often want to open generated reports or logs in a new tab without interrupting the user's current session.

Below is a robust extension class. Notice the use of the using statement pattern. In .NET (and X++), this ensures that the memory stream is disposed of correctly, preventing memory leaks on the AOS.

[ExtensionOf(classStr(File))]
final class My_File_Extension
{
    /// <summary>
    /// Converts a string content to a stream and sends it to a new browser tab.
    /// </summary>
    public static void SendStringAsFileToUserNewTab(
        str _content, 
        str _fileName, 
        System.Text.Encoding _encoding = System.Text.Encoding::get_UTF8(), 
        ClassName _strategy = classstr(FileUploadTemporaryStorageStrategy))
    {
        // Convert string to byte array
        System.Byte[] byteArray = _encoding.GetBytes(_content);
        
        // "using" pattern ensures the stream is closed/disposed after use
        using (System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray))
        {
            File::SendFileToUserNewTab(stream, _fileName, _strategy);
        }
    }

    /// <summary>
    /// Uploads a stream to temp storage and navigates the browser to the URL in a new tab.
    /// </summary>
    public static void SendFileToUserNewTab(
        System.IO.Stream _stream, 
        str _fileName, 
        ClassName _strategy = classstr(FileUploadTemporaryStorageStrategy))
    {
        Browser browser = new Browser();
        str downloadUrl;
        
        // Upload the stream to Azure Temp Storage and get the SAS URL.
        // The 4th parameter 'true' requests a downloadable URL link.
        downloadUrl = File::SendFileToTempStore(_stream, _fileName, _strategy, true);
        
        if (downloadUrl != "")
        {
            // Navigate: (URL, OpenInNewTab = true, ShowLoading = false)
            browser.navigate(downloadUrl, true, false);
        }
        else
        {
            warning("@ApplicationPlatform:DownloadFailed");
        }
    }
}

3. How to Consume This Code

You can now utilize this extension in any Runnable Class (Job) or Service Controller. This is ideal for generating XML logs, simple TXT exports, or CSV files on the fly.

class Job_TestFileDownload
{
    public static void main(Args _args)
    {
        str logContent = "Log Entry: " + DateTimeUtil::toStr(DateTimeUtil::utcNow()) + "\nStatus: Success";
        
        // This will trigger the browser to open a new tab and download 'LogFile.txt'
        File::SendStringAsFileToUserNewTab(logContent, 'LogFile.txt');

        info("Download initiated in background.");
    }
}

4. Summary: Key Takeaways

  • Do not use hardcoded paths: System.IO.Path::GetTempPath() returns a server-side path, which is inaccessible to the client browser directly.
  • Use Streams: For any file larger than a few kilobytes, prefer Streams over Strings to manage AOS memory pressure.
  • Temporary Storage Strategy: The FileUploadTemporaryStorageStrategy automatically handles the cleanup of these files in Azure, so you don't need to write code to delete the temp file after the user downloads it.

Thursday, December 11, 2025

D365F&O: Create and Use Custom Lookups (Updated Guide)

D365F&O: Create and Use Custom Lookups

In this article, we will cover how to create a lookup form and link it with an EDT or String field in Dynamics 365 Finance & Operations using Extensions and Chain of Command.

Technical Prerequisites

  • Chain of Command (CoC): We will use extension classes to override form control behavior.
  • Form Patterns: We will use the "Lookup Basic" pattern to ensure the form renders correctly as a popup in the web browser.

Scenario

We need to override the system lookup to apply custom logic, filters, or use a completely custom Form design as a lookup for a specific field.


1. Standard String/EDT Field Lookup (via Chain of Command)

Use this when you want to filter a specific field using X++ query logic without creating a new custom lookup form.

Class: Form Data Source Field Extension

// Override lookup for CustGroup field
[ExtensionOf(formDataFieldStr(CustTable, CustTable, CustGroup))]
final class CustTableFormDataField_Extension
{
    public void lookup(FormControl _formControl, str _filterStr)
    {
        Query query = new Query();
        QueryBuildDataSource qbds;
        SysTableLookup sysTableLookup;

        // Lookup on CustGroup table (Correct)
        sysTableLookup = SysTableLookup::newParameters(tableNum(CustGroup), _formControl);

        // Fields to show
        sysTableLookup.addLookupfield(fieldNum(CustGroup, CustGroup));
        sysTableLookup.addLookupfield(fieldNum(CustGroup, Name));

        // Build query
        qbds = query.addDataSource(tableNum(CustGroup));

        // Optional filtering example:
        // qbds.addRange(fieldNum(CustGroup, CustGroup)).value("10*");

        sysTableLookup.parmQuery(query);
        sysTableLookup.performFormLookup();
    }
}

2. Reference Group Lookup (RecId-based fields)

This applies when the lookup control is a Reference Group and you want to override its lookup logic.

Class: Form Control Extension

// Example control name: PayrollEarningCode
[ExtensionOf(formControlStr(HcmPosition, PayrollEarningCode))]
final class HcmPosition_PayrollEarningCode_Extension
{
    public Common lookupReference()
    {
        Query query = new Query();
        QueryBuildDataSource qbds;
        SysReferenceTableLookup sysRefTableLookup;

        FormReferenceGroupControl referenceControl = this;

        sysRefTableLookup = SysReferenceTableLookup::newParameters(
            tableNum(PayrollEarningCode),
            referenceControl
        );

        sysRefTableLookup.addLookupfield(fieldNum(PayrollEarningCode, EarningCode));
        sysRefTableLookup.addLookupfield(fieldNum(PayrollEarningCode, QuantityUnit));

        qbds = query.addDataSource(tableNum(PayrollEarningCode));

        sysRefTableLookup.parmQuery(query);

        return sysRefTableLookup.performFormLookup();
    }
}

3. Using a Custom Form as a Lookup

Use this method when you have built a custom lookup form (e.g., CustLookupForm) for complex UI behavior.

Class: Form Control Extension

[ExtensionOf(formControlStr(SalesTable, CustAccount))]
final class SalesTable_CustAccount_Extension
{
    public void lookup(FormControl _formControl, str _filterStr)
    {
        Args args = new Args();
        FormRun custLookup;

        args.name(formStr(CustLookupForm)); // Custom lookup form name
        args.caller(_formControl);

        custLookup = classFactory.formRunClass(args);
        custLookup.init();
        custLookup.run(); // Recommended for stability

        _formControl.performFormLookup(custLookup);
    }
}

How to Design a Lookup Form in D365

A lookup form must follow specific design patterns to render correctly in the browser.

  1. Create the Form: Create a new Form named CustLookupForm.
  2. Apply Form Pattern:
    Right-click the Design node → Apply PatternLookup Basic.
  3. Data Sources:
    • Add CustTable
    • Add DirPartyTable and join it to CustTable
    • Set AllowEdit, AllowCreate, AllowDelete to No
  4. Design Properties:
    Set Style = Lookup.
  5. Grid Layout:
    The Lookup Basic pattern requires:
    Design → Group → Grid
  6. Form Logic (init):
  7. public void init()
    {
        super();
        element.selectMode(CustTable_AccountNum); // Returns the row's value to caller when clicked
    }
    
  8. Form Logic (run): Handle filtering
  9. public void run()
    {
        FormStringControl callerControl;
        boolean filterLookup;
    
        callerControl = SysTableLookup::getCallerStringControl(element.args());
    
        // Filter before run()
        filterLookup = SysTableLookup::filterLookupPreRun(
            callerControl,
            CustTable_AccountNum,
            CustTable_ds
        );
    
        super();
    
        // Apply filter after form loads
        SysTableLookup::filterLookupPostRun(
            filterLookup,
            callerControl.text(),
            CustTable_AccountNum,
            CustTable_ds
        );
    }
    

Monday, December 1, 2025

Dive: Understanding State and Status in the Orchestrator Database

Deep Dive: State and Status in D365 On-Premise (LBD)

Deep Dive: Understanding State and Status in D365 On-Premise (LBD)

In Dynamics 365 Finance & Operations On-Premises (LBD), troubleshooting often requires looking at two independent layers:

  • The Logical Layer (Orchestrator Database)
  • The Physical Layer (Service Fabric Cluster)

Part 1: The Logical Layer (Orchestrator DB)

The Orchestrator defines what should happen. Its core table is Deployment. The state machine here represents the “intended plan.”

1. DeploymentState (What stage are we in?)

These states follow a generally linear lifecycle.

stateDiagram-v2 [*] --> Undefined Undefined --> Preparing: User Clicks Deploy Preparing --> Downloading: Local Agent downloads package Downloading --> Prepared: Package Ready Prepared --> Deploying: Orchestrator starts SF actions Deploying --> Active: Successful Deployment Deploying --> Inactive: Failed Deployment Active --> Deleting: Removing Environment Deleting --> Deleted
Figure 2: Expected flow of Deployment States.
IDState NameDescription
7DownloadingLocal Agent is downloading packages. If stuck, Agent or storage connectivity may be the issue.
2DeployingOrchestrator is executing deployment actions in Service Fabric.
1ActiveEnvironment is deployed and running.

2. DeploymentStatus (How is it going?)

Status indicates whether the Orchestrator encountered errors:

  • None (0): Normal operation.
  • Failed (2): A deployment step failed.
  • StateTransitionIncomplete (3): A state change failed; deployment is stuck.

Part 2: The Physical Layer (Service Fabric)

When the Orchestrator is stuck in Deploying, it is usually waiting for Service Fabric to complete service or replica transitions.

graph TD Cluster --> Node1 Node1 --> Application Application --> Service Service --> Partition Partition --> R1[Replica 1] Partition --> R2[Replica 2] style R1 fill:#ffcccc,stroke:#333,stroke-width:2px style R2 fill:#ccffcc,stroke:#333,stroke-width:2px
Figure 3: Service Fabric Health Hierarchy. A single unhealthy replica (Red) can block Orchestrator transitions.

ReplicaStatus (Common physical blocker)

  • InBuild: Node is copying data; can be slow if network or disk is slow.
  • Standby: Replica assigned but not active.
  • Ready: Replica is healthy and active.

Troubleshooting: Safe SQL Queries

Run ONLY on: OrchestratorDB

Step 1: Check last Orchestrator Command

Confirms whether the system received your deploy/download click.

SELECT TOP 5 
    Id,
    CommandType,   -- 1=Deploy, 9=Download
    CommandStatus, -- 0=Init, 4=Processing, 2=Processed, 6=Failed
    CreatedDateTime,
    ModifiedDateTime
FROM OrchestratorCommand
ORDER BY CreatedDateTime DESC;

Step 2: Check Deployment State

SELECT TOP 1 
    Id,
    DeploymentState,  -- e.g. 7=Downloading, 2=Deploying
    DeploymentStatus
FROM Deployment;
-- Use WHERE Id='your GUID' if multiple environments exist

Step 3: Join with OrchestratorCommand

SELECT TOP 5
    d.DeploymentState,
    c.CommandType,
    c.CommandStatus,
    c.JobDescription

Search This Blog