Executing Dynamics 365 Finance Post Refresh Tasks using Custom Service and D365.Integrations Tools

Currently when copying PROD data to a lower environment like a Tier2 sandbox or a cloud hosted dev box, there is no standard way of how the data copied will be cleansed and how PROD specific endpoints will be updated to point to TEST endpoints. Some customers do it manually(yup!), some using a SQL script(requires JIT access SQL if doing on a tier2 environment) and some using a custom service.

In this post, I will solely be talking about how I was able to accomplish this with a custom service which can be called from an Azure Devops pipeline using a PowerShell script using D365.Integrations tools. This will be a two part process:

Develop a Custom Service in D365

Note that here I will not talk about how to create a customer service in Dynamics 365 Finance. For that Peter has done a splendid job explaining how to accomplish that in his blog post here Create a Custom Service in D365

When creating this service and before running the code to take request object to perform actions, a validation should be added so that the code in this service is never accidently run in a PROD environment.

Below is an example of the service class showing how I did it.

using Microsoft.Dynamics.ApplicationPlatform.Environment;
public class ABCDataCleanupServiceClass
{
public ABCDataCleanupResponse runCleanUpTasks(ABCDataCleanupRequest _request)
{
    CustParameters  custParametersTable;
    var response = new ABCDataCleanupResponse();
    const str tier2sandbox      = "sandbox.operations.dynamics.com";
    const str axcloudmachine = "axcloud.dynamics.com";
    try
    {
        //Start with validating that code only runs in a non-PROD environment
        IApplicationEnvironment env = EnvironmentFactory::GetApplicationEnvironment();
        str currentUrl = env.Infrastructure.HostUrl;
        if (strContains(currentUrl, tier2sandbox) || strContains(currentUrl, axcloudmachine))
        {
            //Code goes here
              changecompany(_request.parmDataAreaId())
            {
                try
                {
                    select forupdate custParametersTable;
                    custParametersTable.MandatoryTaxGroup = _request.parmMandatoryTaxGroup();
                    ttsbegin;
                    custParametersTable.update();
                    ttscommit;
                    response.parmDebugMessage("Param updated"); 
                    response.parmSuccess(true);
                }
                catch (Exception::CLRError)
                {
                    System.Exception interopException = CLRInterop::getLastException();
                    response.parmSuccess(false);
                    response.parmErrorMessage(interopException.ToString());
                }
            }
        }
    }
    catch (Exception::CLRError)
    {
        throw error('This a production environment');
    }
    return response;
}
}

Create an Azure DevOps Pipeline

In an Azure devops pipeline, create a new task of type PowerShell to install required tools. See script below:

Install-PackageProvider nuget -Scope CurrentUser -Force -Confirm:$false
Write-host "NUGET INSTALLED"
 
Install-Module -Name AZ -AllowClobber -Scope CurrentUser -Force -Confirm:$False -SkipPublisherCheck
Write-host "AZ INSTALLED"
 
Install-Module -Name d365fo.integrations -AllowClobber -Scope CurrentUser -Force -Confirm:$false
Write-host "D365.INTEGRATIONS INSTALLED"

Create a separate PowerShell task or use the same as per your need to call the custom service using Invoke-D365RestEndpoint. Note that before this custom service can be accessed by the script, enable the Azure App ClientID in D365 by going to Azure Active Directory Applications and assign it to a service account with system administrator role. To register the app in Azure, this blog has step by step instructions on how to do it. App Registration in Azure

Finally, below is an example for the script that has the request parameters in form of a JSON and passed as Payload to the function Invoke-D365RestEndpoint

$Payload = '{
    "_request" :
    {
        "DataAreaId": "USRT",
        "MandatoryTaxGroup": "true"
    }
}'
$oDataToken = Get-D365ODataToken -Tenant $Tenant -Url $D365EnvironmentURL  -ClientId $ClientId -ClientSecret $ClientSecret #-RawOutput -EnableException
Invoke-D365RestEndpoint  -ServiceName "ABCDataCleanupServiceGroup/ABCDataCleanupService/runCleanUpTasks" -Payload $Payload -Tenant $Tenant -Url $D365EnvironmentURL  -ClientId $ClientId -ClientSecret $ClientSecret  -Token $oDataToken

Payload variable here contains parameters that will be passed to the custom service in D365 as a JSON

D365EnvironmentURL here will be the D365 environment where these tasks are to be performed

oDataToken here is being generated for D365 URL using the function Get-D365ODataToken included in d365.integations tools

Once the script is run in this example, it would have updated this parameter

Parameter updated in D365

Are you doing something similar or have any suggestions to improve this further? Let me know in comments.

MacOS + Dynamics 365/Power Platform…whats missing?

Without going into the debate of which OS is better for a desktop computer, you may find yourself in a situation where you are using MacOS and involved in work that is related to Microsoft technologies like Dynamics 365 and/or Power Platform and wonder if everything will work in similar way as it would on a Windows. To answer that, in my experience below are few things that I found which had an Windows OS requirement and I had to switch either to a spare Windows that I had or use a VM.

Workflow Editor for Dynamics 365 Finance & SCM

When dealing with D365 Finance and Operation(or new names Finance and Supply Chain Management), you may have to build workflows for certain business processes. To be able to do that Workflow Editor is used which actually is a one-click application which won’t work on a MacOS.

Open in Excel

If looking to manipulate any data by open a form/list page in Excel on MacOS, you’ll encounter an error saying ‘This add-in is currently not available for use on your device’. This functionality/add-in is also only available to work on Windows.

Management Reporter Designer

Recently while working on a request from finance team to modify one of their financial report I stumbled upon a similar to workflow editor limitation where the management reporter designer won’t open on a MacOS computer and required Windows

Regression Suite Automation Tool (RSAT)

With all the ever growing need to customize D365 Finance, SCM and MPOS/CPOS to handle specific business scenarios comes a challenge where regression testing may become hectic and super time consuming. Thankfully Microsoft has a tool to automate such testing using the RSAT tool (which I personally love) but if you plan to use it on MacOS, you’re out of luck. RSAT requires Windows to run.

Power Automate Desktop

Isn’t it fun to see how Power Platform is evolving? And how easily several of your legacy repetitive tasks can so easily be automated so you can focus on other stuff? If you’re like me, you would have come across a task or two where you may have had to automate a process on a computer instead of running it on Power Automate as a cloud flow. If yes, Power Automate Desktop(it’s pretty slick and worth checking if you haven’t so far) is your tool however to use this, Windows OS will be required.

For me, thankfully it was just the development part where I ran into this requirement so I just used a Windows computer instead to develop and then my actual computer where the tool had to run was already a Windows.

Visio

Though a general tool, worth mentioning here as while working with D365 one may need to have a visual representation of business processes and architecture. For this purpose what better tool then Visio! Though some alternate tools are available for MacOS and even Power Point to some extent, I still felt the gap.

Have you come across anything that did not work for you on MacOS? Let me know in comments.