AX / D365FO – Call Power Automate / Logic App from X++ code

This is a simple example of how to call external web services from D365FO without using any C# DLL files. Just plain X++ code. As sample External Web Service, I have selected Microsoft Flow. Azure Logic Apps will be similar.

Create a test Microsoft Flow

Let’s create a simple Microsoft Flow that will be triggered by a POST method. Receive some JSON as an input parameter. Do nothing. And then, Send a 200 response with some JSON text.

Please find below the full Microsoft Flow text

Please find animated GIF below how to create this Microsoft Flow

X++ code

main() method

Just create an instance of the class and call the makePOST() method.

makePOST() method

Execute all logic inside X++. Please find the main points of interest below.
• URL – the endpoint of our Microsoft Flow. The address that you have copied from Microsoft Flow after saving.
• requestContent – JSON text that we are going to send to Microsoft Flow
• configureHttpClient() – method to configure HTTP call

configureHttpClient() method

Prepare and configure HTTP call
• Enable TLS 1.2 security protocol
• Validate URL
• Create request. The instance of HttpRequestMessage class. With method POST.
• Update header. Add a correct content type to pass JSON as an input parameter.

X++ code

Please find sample X++ code below

using System.Net;

using System.Net.Http;

/// <summary>
/// How to call Microsoft Flow / Logic App from X++ code
/// </summary>
class CiePOSTmsFlow
{
private HttpRequestMessage request;
private System.Net.Http.HttpClient httpClient;
private str requestContent;

/// <summary>
/// main method
/// </summary>
/// <param name = "_args"></param>
public static void main(Args _args)
{
CiePOSTmsFlow postClass = new CiePOSTmsFlow();

postClass.makePOST();
}

/// <summary>
/// Make a POST call to specified URL
/// </summary>
public void makePOST()
{
System.Net.Http.HttpResponseMessage response;
const str URL = 'https://prod-38.westus.logic.azure.com:443/workflows/92375d07a20f49f0af0c6ef5ecdcd0dc/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=xUL92bTtRiS4bGK0BEscSmpZoryU8UOxL52uNQR_bto'; //Microsoft Flow / Logic App endpoint

try
{
requestContent = '{"name" : "JSON parameter1", "description" : "Test JSON" }'; //test JSON
this.ConfigureHttpClient(URL);

if(request != null)
{
response = httpClient.SendAsync(request).Result;

if(!response.IsSuccessStatusCode)
{
Info("MS Flow response is not successful. Please find details below.");
warning(strFmt(response.Content.ReadAsStringAsync().Result));
}
else
{
Info("MS Flow has been called successful");
Info(strFmt(response.Content.ReadAsStringAsync().Result));
}
}
}
catch
{
warning("Error in calling MS Flow");
}

}

/// <summary>
/// Configure HTTP Client
/// </summary>
/// <param name = "_url">URL</param>
private void ConfigureHttpClient(str _url = '')
{
System.Net.ServicePointManager::set_SecurityProtocol(System.Net.SecurityProtocolType::Tls12);

httpClient = new System.Net.Http.HttpClient();
if(!_url)
{
warning("No URL Provided");
}
else
{
httpClient.BaseAddress = new System.Uri(_url);
System.Net.Http.Headers.HttpHeaders requestHeaders = httpClient.DefaultRequestHeaders;

request = new HttpRequestMessage(HttpMethod::Post, httpClient.BaseAddress);

if(requestContent)
{
request.Content = new StringContent(requestContent);
requestHeaders = request.Content.Headers;
requestHeaders.Remove("Content-Type");
//https://www.json.org/index.html
//A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string.
//The application/json entry in the IANA registry has a note:
//Note: No "charset" parameter is defined for this registration.
//Adding one really has no effect on compliant recipients.
requestHeaders.Add("Content-Type", "application/json");
//requestHeaders.Add("Ocp-Apim-Subscription-Key", sKey);
requestContent = "";
}
}

}

}

Test run

Let’s compile it and run. First run should be successful. If not, please make sure that you have copied URL address from Microsoft Flow to your X++ code.

Let’s test failure scenarios as well. Switch off Microsoft Flow and run our class again. You should see error message and receive response from Microsoft Flow.

Leave a comment

Blog at WordPress.com.