Wednesday, August 15, 2018

The new Messaging API in Dynamics 365 for Operations

Introduction

With the release of Microsoft Dynamics 365 for Finance and Operations, many features have been revamped, including the Messaging API. Even though support for legacy API (info(), warning()/checkFailed(), and error()) still exists, they are now a part of the framework's new messaging system. The new messaging system exposes a Message class that allows you to Add or Remove messages from the user interface. This is a powerful feature since it can be used to reflect the current state of the system. Therefore, this API is useful for enhancing the user's experience who is using the application.

The Add method's signature is as:
public static int64 Add(MessageSeverity severity, SysInfoLogStr txt);
The Remove method's signature is as:
public static int64 Remove(int64 messageId);
Where, MessageSeverity is an enumeration that identifies the type of message being logged. The possible values are Informational, Warning, and Error.

Lets see how we can actually use the new Message class.

Demonstration

The example below demonstrates how the new Add and Remove methods work, and how that code reflects the form's current state.
public class MessageAPI 

    public static void main(Args _args) 
    { 
        // Adding messages to infolog stack 
        Message::Add(MessageSeverity::Informational, "Information Message"); 
        Message::Add(MessageSeverity::Warning, "Warning Message"); 
        Message::Add(MessageSeverity::Error, "Error Message"); 

        // Adding message to infolog stack, but pertaining its message Id 
        int64 noMsg = Message::Add(MessageSeverity::Informational, "This message won't be displayed"); 

        // Removing message with messageId identifier 
        Message::Remove(noMsg); 
    } 
}
The result of running MessageAPI class is as:



The demonstration above shows how messages can be logged onto the infolog window. The program when executed, pushes 4 messages to the user interface message stack via Add method. After that we call the Remove method to remove one particular message from the stack, leaving us with 3 messages (Information Message, Warning Message, and Error Message). Finally, when the program execution is completed, it displays 3 messages that are shown in the screenshot above.

Conclusion

As you can see from this simple demonstration. The new Message API controls how and when to display certain messages. The Add method pushes a message to the infolog stack while returning its unique Id, and the Remove method removes a particular message with a specific MessageId. The provides us with dynamic control over how and when to display certain message on the user interface.

No comments:

Post a Comment

Copying a Record using data() method in D365 for Operations

Introduction Data is an essential pillar of an Enterprise. Therefore, Microsoft Dynamics 365 for Finance and Operations provides various ...