This article is an extension to “Create a Dialog Short-Cut Ribbon Button” tutorial provided by develop1.
In the original article, we use ribbon workbench to add short-cut to a dialog using a button. What if we want to reflect data or updated status on the form, during or after execution of this dialog? That simply means, we do need a form refresh once dialog is finished/closed.
Problem
Launch a dialog from a button in the command bar and refresh form once dialog is finished/closed.
Solution
Fortunately, we have a very simple solution to this problem but it is un-supported. Although I tested it with CRM 2013, 2015 and 2016.
I am dividing this solution into 3 major but very simple steps:
- Add a JavaScript webresource to call dialog and refresh the form afterwards.
- Extract Dialog ID.
- Add a button to form’s command bar.
Please note, If you have already followed original article, you just need to see step 1.
Step 1: Add a JavaScript webresource to run dialog and refresh form:
In this particular example, I am going to work on case entity. So first I created a CRM Solution and added Case entity to it. You need to create a new webresource using following steps:
- Add a new webresource to solution and set the type to Script (JScript).
- Add following script to this web resource:
// Java Script code
CRMLogs_RunDialogAndRefresh = function (objectTypeCode, dialogId) {
// Run Dialogvar 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;// use ‘openStdDlg’ instead of ‘openStdWin’ in order to launch dialog as a modal dialogif (typeof (openStdDlg) == ‘undefined’) {
hostWindow = window.parent; // Support for Turbo-forms in CRM2015 Update 1
}if (typeof (hostWindow.openStdDlg) != ‘undefined’) {openStdDlg(rundialog, null, 615, 480, true, false, “maximize:yes;minimize:yes”);
}// Refreshes page once dialog is finished/closed
Xrm.Utility.openEntityForm(“incident”, primaryEntityId);}Please note, I have used “hostWindow.openStdDlg()” function instead of “hostWindow.openStdWin”. openStdDlg() opens the CRM dialog window as a modal window, that means user can’t move back to form until dialog is finished or closed. - Give a name to webresource like ‘/Scripts/RunDialog’ and then save, publish and close.
Step 2: Extract Dialog ID from Dialog:
To extract dialog id follow these steps:
- Open the Dialog you want to run. Click Actions –> Copy Link. Paste copied link to the notepad and you will see something like this:
<http://<server>/<OrgName>/sfa/workflow/edit.aspx?id=%7b<Dialog ID>%7d>. - Extract the dialog id and save it for later.
Step 3: Add a button to form’s command bar to launch dialog:
- Launch Ribbon Workbench and load your solution.
- In the ‘Solution Element’ panel, add the following EnableRule and mark ‘IsCore=True’ in the properties:-Mscrm.FormStateNotNew
This will ensure that the button is only enabled record is existing. - Under Command in the Solution Elements Panel, add a command like:-new.incident.RunDialogForm.Command
- Right-Click on the Command ‘new.contact.RunDialogForm.Command’ and select ‘Edit Enable Rules’.
- Add the following Enable Rule:-Mscrm.FormStateNotNew
This will ensure that the button is only enabled record is existing. - Righ-click on the Command ‘new.incident.RunDialogForm.Command’ and select ‘Edit Actions’
Add a Javascript Command and set the following properties:Function Name CRMLogs_RunDialogAndRefresh Library Use the lookup button to select the webresource you created above Parameters 1: Type = Crm Parameter, Value = PrimaryEntityTypeName
2: Type = String, Value = Dialog ID found from the Copy Link in the previous steps - Select the Form ribbon and drag a new button into the ‘Process’ Group or any other group. Give a meaningful name to this button like ‘Run Settlement Dialog’.
- Select above created command for this button and click ‘Publish’.
That’s all, you did it!
*This post is locked for comments