Friday, May 13, 2016

Inject your code before CRM button event - Conditional deactivation of CRM record

I came across business requirement like, if there are any active child records, then show error message to users when deactivating parent record.


I can use plugin or real time workflow for this, but when error message thrown, CRM throws error like Business process error.


But customer doesn’t want to see this business process error, customer wants Alert message, and do not want to deactivate record.
To full fill this requirement I customized CRM button command, and added my own script on deactivate button.
Here are steps I did

I am using Ribbon workbench to customize entity ribbon.

1.       Customize Deactivate command

2.       Once command is customized you will see JavaScript library and function used by MS CRM.



3.  Change function Name to NaN and remove parameters for CRM default deactivates action.

This we need to do so when condition is satisfied, record should be deactivated and CRM JavaScript library should be loaded. 


4.       Now add your own JavaScript action in deactivate command, and call your own JavaScript function
When calling own JavaScript function make sure you are using same Parameters used in CRM default deactivates function, as we are going to call this function in our own JavaScript.







5.       In your JavaScript code, you can check, is there any active child records associated with this record? If not then deactivate record, otherwise show message. 

JavaScript code is

CheckOnDeactivation: function (CompanyId, entityName) {
        var req = new XMLHttpRequest();
        req.open("GET", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/contact?$select=contactId&$filter=parentcustomerid/Id eq (guid'" + CompanyId + "') and statecode/Value eq 0", false);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                this.onreadystatechange = null;
                if (this.status === 200) {
                    var returned = JSON.parse(this.responseText).d;
                    var results = returned.results;
                    if (results.length >= 1) {
                        alert('You cannot deactivate this record because it has active child records.');                        
                        return false;
                    }
                    else {
                        Mscrm.CommandBarActions.deactivate(CompanyId, entityName);
                        return true;
                    }
                }
                else {
                    alert(this.statusText);
                }
            }
        };
        req.send();
    }

6. Save and Publish customization
Now when trying to deactivate parent if it has any active child then error will be thrown and record will not be deactivated. 

You can use same technique for any other button command.  

2 comments:

  1. Nice post and gives in depth information. Thanks for this great post. CRM For Real Estate Sales

    ReplyDelete
  2. The use of this command gives a confirmation popup to ask whether you would like to deactivate the record.
    How to bypass that and directly deactivate the record?

    ReplyDelete