CRM 2015/2016 (and earlier) – Running a dialog from a button WITH form refresh

Clients love dialogs. They’re a really great way to guide users through a process, and make sure that the process happens exactly the right way, every time. But, clients also hate extra clicks, and are almost always annoyed that they have to click the “Start Dialog” button, select the correct dialog from a group of them, and then start clicking through the dialog. They also hate when the form doesn’t automatically refresh, and show their changes when they’re done.

I’m almost always asked “Can’t you just create a button that does this?” To which the answer has always been “Yes, but it’s unsupported”. I don’t know why Microsoft hasn’t given us this function out of the box yet, or at least a supported way to do it ourselves. But for whatever reason, they haven’t.

But unsupported doesn’t mean “never”. It means it might break in a future release, and you should have that conversation with your clients ahead of time. In my experience, clients like the custom buttons that call dialogs so much, they’re willing to allow the unsupported code.

Again, take note, Microsoft.

Ok, so disclaimers out of the way, on to the good stuff. How can you call your dialogs from a button and automatically refresh the form afterwards? I’m actually just synthesizing several solutions put out there by other people. However, since I had to search for hours to find them all and several more to make them work together, there seems to be value in putting the information in one place.

Step 1: Implement Scott Durow’s Ribbon Workbench solution to add a button to call a dialog. Get it working. It works perfectly. Here’s the link: https://ribbonworkbench.uservoice.com/knowledgebase/articles/140652-create-a-dialog-short-cut-ribbon-button

Now, you should have a button that calls a dialog. Until this is working, do not move forward.

The problem you now have is that if the dialog updates the form, clients won’t see the updates until they manually refresh, which usually results in the following conversation:

Client: I ran the dialog and it completed, but it didn’t work.
You: Yes it did. You just have to manually refresh the form.
Client: Really? That stinks.
You: Yeah, it does.

Wouldn’t it be nice if you could automatically refresh after the dialog runs? Which leads me to:

Step 2: Go to codeplex, and download the Alert.js CRM solution. It contains a JavaScript function called Alert.showDialogProcess. This is a great function that shows the dialog URL in an Iframe. Here’s a link: https://alertjs.codeplex.com/ (Go to the “Downloads” tab)

Now, you should have downloaded a solution you can import into CRM.

Step 3: Import the solution into CRM. This installs a series of Web Resources that give you access to the relevant JavaScript functions.

Step 4: Open the form that you added the button to in step 1. Add the //js/alert.js web resource to the form. This is done from the Form Properties button:

DialogRefresh1

DialogRefresh2

Step 5: Back in step one, you created a Javascript function to call from the button on the form. If you followed the Ribbon Workbench instructions, it looks like this:

Develop1_RibbonCommands_runDialogForm = function(objectTypeCode, dialogId) {
    var primaryEntityId = Xrm.Page.data.entity.getId();
    var rundialog = Mscrm.CrmUri.create('/cs/dialog/rundialog.aspx');
    rundialog.get_query()['DialogId'] = dialogId;
    rundialog.get_query()['ObjectId'] = primaryEntityId;
    rundialog.get_query()['EntityName'] = objectTypeCode;
    var hostWindow = window;
        if (typeof(openStdWin) == 'undefined') {
            hostWindow = window.parent; // Support for Turbo-forms in CRM2015 Update 1
        }
        if (typeof(hostWindow.openStdWin) != 'undefined') {
            hostWindow.openStdWin(rundialog, hostWindow.buildWinName(null), 615, 480, null);
        } 
}

Just update the function to look like this:

Develop1_RibbonCommands_runDialogForm = function(objectTypeCode, dialogId) {
    var primaryEntityId = Xrm.Page.data.entity.getId();
	Alert.showDialogProcess(dialogId, objectTypeCode, primaryEntityId,
			function () {
				Xrm.Utility.openEntityForm(objectTypeCode, primaryEntityId);
			});
	}
}

This uses the Alert.showDialogProcess function to open the dialog, and call the Xrm.Utility.openEntityForm function as a callback to reopen the form.

And that’s it. Your button should now call a dialog, and then refresh/reopen the form when it’s finished.

Try it. Your clients will love it.

One thought on “CRM 2015/2016 (and earlier) – Running a dialog from a button WITH form refresh

Leave a Reply

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