Showing an Error through JavaScript in Power Apps and Dynamics 365

Leave a comment

When customizing model-driven Power Apps and Dynamics 365 with JavaScript, you may need to display an error to the user. One way to do this is to use Xrm.Navigation.openErrorDialog, which is called like below:

Xrm.Navigation.openErrorDialog(errorOptions).then(successCallback,errorCallback);

Let’s run it in browser Development Tools to show how it works. Open a Power App and in dev tools add the following code:

Xrm.Navigation.openErrorDialog({ errorCode:"ABC7123", details:"These are the details that will go into the downloaded file", message:"Here's the message" }).then(
    function (success) {
        console.log(success);        
    },
    function (error) {
        console.log(error);
    });

Let’s run this:

We see we get our Message as well as an option to Show Details:

On clicking Show Details, we get our Error code and other useful information:

We also get a link to download the log file:

And when opening this, we get the details we specified:

Note from the Microsoft documentation we get the following options:

NameTypeRequiredDescription
errorOptionsObjectYesAn object to specify the options for error dialog. The object contains the following attributes:
– details: (Optional) String. Details about the error. When you specify this, the Download Log File button is available in the error message, and clicking it will let users download a text file with the content specified in this attribute.
– errorCode: (Optional) Number. The error code. If you just set errorCode, the message for the error code is automatically retrieved from the server and displayed in the error dialog. If you specify an invalid errorCode value, an error dialog with a default error message is displyed.
– message: (Optional) String. The message to be displayed in the error dialog.You must set either the errorCode or message attribute.
successCallbackfunctionNoA function to execute when the error dialog is closed.
errorCallbackfunctionNoA function to execute when the operation fails.

A useful feature if you want to handle errors in this way.

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *