Tip #20: Use alertDialog and confirmDialog to display messages to users

If your javascript code is peppered with alerts and confirms, it’s time to stock up on the invigorating drink of your choice and rewrite. The reason is very simple – CRM for tablets does not like anything that blocks execution. New stablemates are alertDialog and confirmDialog – use them to display messages to users and set code to execute based on their response. Because of the asynchronous nature, the signature of the functions is different and involves callbacks:

    Xrm.Utility.alertDialog(message, onCloseCallback)

    Xrm.Utility.confirmDialog(message, yesCloseCallback, noCloseCallback)


On tablets any use of alert in the existing code will be automatically overwritten to use alertDialog function. So why all the fuss then? alertDialog is asynchronous on the tablets and code execution will continue, something that 99% of the existing code probably does not expect. That, and a little thing called backward compatibility. According to SDK, “This mapping of window.alert to Xrm.Utility.alertDialog in CRM for tablets is deprecated and will be removed in the next major release.

The following sample demonstrate use of the both functions by prompting user if they want to set the description automatically and then does the job (we assume that the form has a field called description).

    FormOnLoad = function () {
        var description = null;

        Xrm.Utility.confirmDialog("Set meaningless description?",
            function () {
                description = "Meaningless description";
            },
            function () { }
        );

        Xrm.Utility.alertDialog(
            "Your wish is my command",
            function () {
                if (description) {
                    Xrm.Page.getAttribute("description")
                    .setValue(description);
                }
            }
        );
    }

And this sample clearly demonstrates asynchronous nature of the code – it simply does not work as expected on tablets – on iPad alert is displayed first followed by confirmation dialog. Working version of the code:

    FormOnLoad = function () {
        Xrm.Utility.confirmDialog("Set meaningless description?",
            function () {
                Xrm.Utility.alertDialog(
                    "Your wish is my command",
                    function () {
                        Xrm.Page.getAttribute("description")
                        .setValue("Meaningless description");
                    }
                );
            },
            function () {}
        );
    }

3 thoughts on “Tip #20: Use alertDialog and confirmDialog to display messages to users

  1. Jonas Rapp says:

    How about the javascript prompt function?
    Is there any way of solving the prompt need without redesigning completely to use dialog processes?

    • From latest SDK:

      CRM for tablets does not allow any functions that can block execution of scripts
      … Using window.confirm, window.prompt, or any other native window function that blocks execution of scripts will throw an error.

      It will probably work OK in the browsers if you don’t care about tablets but UX with prompt is horrible anyway, I suggest re-thinking the entire prompting process, because you already have a good input mechanism right there, i.e. form. For example, consider dynamically altering form behaviour so that user can enter appropriate values on the form instead of ugly javascript prompt.

      • Michael Blackburn says:

        Often the need for a prompt is in response to a user clicking a ribbon button. We’d like to capture a feedback or text input, and rather not redirect them back into the form, and then have them click the button again. Also, it’s possible the response doesn’t *belong* in the form. This is so typical, they take away 100% functionality, give back 66%.

Leave a Reply

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