Tuesday, January 29, 2019

Refresh form after Dialog Process complete in MS CRM


How to refresh the form after Dialog Process complete in MS CRM?

The simple answer to this question is to have invoked a Dialog via Custom button and Jscript and do the refresh from within Jscript code. Here is a few line of code to achieve the same. 

Here I am using Alert.js v2.1 - Copyright Paul Nieuwelaar Magnetism 2016

function CallDialogProcess()
{   
    var recordId = Xrm.Page.data.entity.getId();
    var dialogId = "FD8B50BD-41A6-42D9-B00A-7BD9ED9BEBFB";
    var entityLogicalName = "contact"; 

    Alert.showDialogProcess(dialogId, entityLogicalName, recordId,
        function () { Xrm.Page.data.refresh();
    });
}

But now the challenge comes as when you might promote your code to Production or UAT environment. Putting Hard coded Guid will give you trouble at critical time, especially when going live with certain deadlines.

Now we need a simple Rest API query to get the relevant Dialog/Workflow Id, so we can pass it as a parameter.


Here in this post, I am suggesting a method which will help us achieve our goal without hardcoding dialog Guid.


function CallADialogProcess()

{
var dialogName = 'My Dialog Process Name';  // Provide Name of your Dialog Process here...

var selectClause = '$select=name,workflowid,_parentworkflowid_value,statecode';
var filterClause = "&$filter=statecode eq 1 and _parentworkflowid_value ne null and name eq '" + dialogName + "'";
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/workflows?" + selectClause + filterClause, false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            var i = 0;
            if ((i + 1) === results.value.length)
            {
                   
                var _parentworkflowid_value = results.value[i]["_parentworkflowid_value"];
                   

                var recordId = Xrm.Page.data.entity.getId();
                Alert.showDialogProcess(_parentworkflowid_value, "contact", recordId, function () {
                    Xrm.Page.data.refresh();
                });
            }
        }
        else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();
}

Please Note the highlighted code part, where we are actually fetching the Dialog Guid.

I love contributing what I am learning J

Regards,
Vipin Jaiswal
Vipinjaiswal12@gmail.com

No comments: