Call a simple Logic app from X++

Logic apps can be called manually from D365FO with code

I've made a basic Logic App that receive a short JSON.


1. Get JSON schema

Get your JSON sample and go here and paste it in https://www.liquid-technologies.com/online-json-to-schema-converter. Press Generate Schema and you have your schema in the box below.

Example JSON :

{
  "sku": "875828",
  "externalMaterialReference": "400267"
}

Example JSON Schema

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "sku": {
      "type": "string"
    },
    "externalMaterialReference": {
      "type": "string"
    }
  },
  "required": [
    "sku",
    "externalMaterialReference"
  ]
}


2. Create Logic App

In the Azure Portal or in your Visual Studio, create a new blank Logic App. Add an similar logic as above starting with When a HTTP request is reseived. After saving the logic app copy the HTTP POST URL and save it.

3. Create X++ code to call logic app.

The code under simply call the logic app with a JSON consisting of two parameters. 

using System.Net;
using System.Net.Http;

class CiePOSTFlow
{     

    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)
    {
        CiePOSTFlow   postClass = new CiePOSTFlow();

        postClass.makePOST();
    }

    /// <summary>
    ///     Make a POST call to specified URL
    /// </summary>
    public void makePOST()
    {
        System.Net.Http.HttpResponseMessage response;
        //const str URL = '[Your HTTP POST URL]'; //past in your URL HERE

        try
        {
            requestContent = '{"externalMaterialReference" : "ExMR987654",  "sku" : "SKU123456"   }'; //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 = "";
            }
        }

    }
}

Running the code results in this, simply contcatinates the two parameters into one.

    

Comments

Popular posts from this blog

Retail Attributes on sales lines

Customer payment transaction, mark for settlement against invoice and post