Power Community

Power Community

Call a Machine Learning model from Business Central

This post is a follow up to this one:

(99+) Creating a Machine Learning model in Azure – beginner’s step by step guide – Dynamics 365 Business Central Community

Here, we call the model from Business Central, and not from Postman.

REST

In this post we assume that you have already created a ML model in Azure, as described in the post linked above. To activate it we just run a REST call – here we send a parameter in the Body section. We just send “Credit Limit (LCY)” from a customer card to keep things simple. Here is the code needed – once you have deployed it, then go to a Customer Card, enter an amount in “Credit Limit (LCY)”, then select action Related -> Customer -> “Run ML model”, and the model will do what it does (multiplies by 2), and then send the result back.

Please do not be shy to suggest better code for making REST calls, but here is one version:

pageextension 50101 CustCardExt extends “Customer Card”

{

    layout

    {

        // Add changes to page layout here

    }

    actions

    {

        // Add changes to page actions here

        addlast(“&Customer”)

        {

            action(RunML)

            {

                Caption = ‘Run ML model’;

                ApplicationArea = all;

                Image = MachineCenter;

                trigger OnAction()

                var

                    myInt: Integer;

                begin

                    Message(‘Hello.’);

                    RunMLCodeunit.RunModel(rec.“Credit Limit (LCY)”);

                end;

            }

        }

    }

    var

        myInt: Integer;

        RunMLCodeunit: Codeunit RunML;

}

codeunit 50101 RunML

{

    trigger OnRun()

    begin

    end;

    procedure RunModel(Input: Integer)

    var

        HTTPClient: HttpClient;

        ResponseMessage: HttpResponseMessage;

        myInt: Integer;

        URL: Text;

        RequestHttpContent: HttpContent;

        Txt: Text;

        Payload: Text;

        GlobalNumber: Decimal;

    begin

        URL := ‘http://1237ef2a-1a05-4e12-8889-1f078a5abcd8.uksouth.azurecontainer.io/score’;

        HTTPClient.DefaultRequestHeaders.Add(‘User-Agent’, ‘Dynamics 365’);

        httpClient.DefaultRequestHeaders.Add(‘Accept’, ‘application/json’);

        HTTPClient.DefaultRequestHeaders.Add(‘Authorization’, ‘Bearer hVzABCsutkc0l2nChyrCB66QxfYQGaWn’);

        Payload := ‘{“Inputs”: {“input1”: [{“Number”: ‘ + format(Input) + ‘}]},”GlobalParameters”: {}}’;

        RequestHttpContent.WriteFrom(Payload);

        HTTPClient.Post(URL, RequestHttpContent, ResponseMessage);

        ResponseMessage.Content.ReadAs(Txt);

        Message(Txt);

    end;

    var

        myInt: Integer;

}

- Advertisement -spot_img

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img
- Advertisement - Advertisement

Latest News

What’s New in the September Release of Power BI Report Server

This release introduces Power BI Report Server (PBIRS) for SQL Server 2022. We continue to innovate, create, and design...

More Articles Like This

- Advertisement -spot_img