Showing posts with label JSON format. Show all posts
Showing posts with label JSON format. Show all posts

Saturday, April 11, 2020

Message API - Message::AddAction() in D365FO Version 10.0.10 PU34 | New Feature

Introduction: 
From the version 10.0.10 Platform update 34, Microsoft has added a new feature Message::AddAction() which is shown in the message bar. 

Details: 
Message API associated with display or action menu items, which is visualized as a hyperlink/link button. 

It is linked with a single record at a time, called single action. 

In below taken example, we will show sales order is navigated to the form SalesTable from the message bar. 

For testing it, we'll create the runnable class also know as job in AX 2012. 
class CFSMessageAPI
{
    public static void main(Args _args)
    {
        SalesTable            salesTable = SalesTable::find('SH-000121');
        MenuItemMessageAction actionData = new MenuItemMessageAction();

        actionData.MenuItemName(menuItemDisplayStr(SalesTable));
        actionData.TableName(tableStr(SalesTable));
        actionData.RecId(salesTable.RecId);
        str jsonData = FormJsonSerializer::serializeClass(actionData);

        int64 messageId = Message::AddAction(MessageSeverity::Informational, "Sales order details", salesTable.customerName(), MessageActionType::DisplayMenuItem, jsonData);
    }

}
Let's see how it works, 









In my case it's showing Mitchell. Click on the link. 



















After clicking on the action link, it is navigated to the sales order record form as shown in the above link. 

Hurray, How pretty feature is released !!!

Conclusion:
In above example, we have seen how Message API is routed to the record. 

Thanks for reading !!!

Thursday, April 9, 2020

Rest API GET call in JSON format in Dynamics 365 Finance and Operations

Introduction:
In this blog, we will see how to get response from Rest Api through GET call


Solution: 
Consisting of basic authentication, we will pass username and password in byteStr and for the endpoint we will put it in url in below code.
class CFSJSTestRestAPI
{
    public static void main(Args _args)
    {
        int                                find;
        str                                url,aosUri,activeDirectoryTenant;
        str                                activeDirectoryClientAppId;
        str                                activeDirectoryClientAppSecret;
        str                                postData,activeDirectoryResource;
        str                                aadClientAppSecret,oAuthHeader;
        str                                returnValue,jsonString,jsondszstr;
        System.Net.HttpWebRequest          request;
        System.Net.HttpWebResponse         response;
        System.Byte[]                      byteArray;
        System.IO.Stream                   dataStream;
        System.IO.StreamReader             streamRead;
        System.IO.StreamWriter             streamWrite;
        System.Net.ServicePoint            servicePoint;
        System.Net.ServicePointManager     servicePointmgr;
        System.Net.HttpVersion             version;
        CLRObject                          clrObj;
        Newtonsoft.Json.JsonReader         reader;
        System.Text.Encoding               utf8;
        Counter                            countCounter;
        Object                             obj;
        Map                                data;
        System.Byte[]                      byteArraynew;
        System.Net.WebHeaderCollection     headers = new System.Net.WebHeaderCollection();
     
        new InteropPermission(InteropKind::ClrInterop).assert();
  
        str  byteStr = strfmt('%1:%2', "USERNAME", "PASSWORD");
        
        headers  = new System.Net.WebHeaderCollection();
        url      = "http://dummy.restapiexample.com/api/v1/employees";
        clrObj   = System.Net.WebRequest::Create(url);
        request  = clrObj;
        request.set_Method("GET");
        request.set_KeepAlive(true);
        request.set_ContentType("application/json");
        utf8            = System.Text.Encoding::get_UTF8();
        byteArraynew    = utf8.GetBytes(byteStr);
        byteStr         = System.Convert::ToBase64String(byteArraynew);

        headers.Add("Authorization", 'Basic ' + byteStr);
        request.set_Headers(headers);

        servicePoint = request.get_ServicePoint();

        System.Net.ServicePointManager::set_Expect100Continue(false);
        System.Net.ServicePointManager::set_SecurityProtocol(System.Net.SecurityProtocolType::Tls12);

        response    = request.GetResponse();
        dataStream  = response.GetResponseStream();
        streamRead  = new System.IO.StreamReader(dataStream);
        jsonString  = streamRead.ReadToEnd();

        info(strFmt("RESPONSE: %1",jsonString));

        dataStream.Close();
        response.Close();
    }
}

Thanks for reading !!!