Showing posts with label Debugging. Show all posts
Showing posts with label Debugging. Show all posts

Monday, 20 March 2017

How to debug a CRM Online Plugin?


When you develop plugins for Dynamics CRM unless it is a very simple scenario 9 out of 10 situations you will require to debug your plugin. One way of fulfilling this requirement is using the tracing which is bit easier. But sometimes this is not sufficient. So you have no other choice but debug the plugin!

With Dynamics CRM online debugging a plugin is not very straight forward. With On-Premise environments you can run the remote debugger on the CRM server and attach to the process in you visual studio and proceed. But in the cloud version with the limited access this is not possible. So here is how you do it:

Start Plugin Registration Tool in the SDK and connect to your CRM organisation.

If this is the first time someone is going to debug a plugin in this organisation you will see the button “Install Profiler” in the menu bar. (Otherwise profiler will be installed already) Click on it and it will start installing profiler. 





Then register you plugin and the steps. Select the step which you want to debug and click "Start Profiling".


































Here you select the option “Exception” and click Ok.


Now in CRM perform the operation where your plugin is supposed to trigger. (In this example it triggers when multiple accounts are retrieved. So I open the Accounts view).

You will get a business process error. Click Download Log File and save the log file into your local drive.














Then open your plugin code in visual studio; set the break points; click Attach to process under the debug menu. Select the PluginRegistration.exe from the available processes and click attach.


Then in the plugin registration tool click “Debug” from the menu.
For the Profile browse to the downloaded error log file and in the previous step and select it.
Select your plugin dll for the Assembly location and then the plugin.

Then click Start Execution.




Debugger will hit your breakpoints you had set up in Visual Studio and you can continue debugging your code with pressing F10.

Once you are done with debugging make sure you go back to the plugin registration tool; select the step in your plugin and click “Stop Profiling”; otherwise you will always get business process error when you perform your operation!


Happy debugging J 



How to call a plugin from a JavaScript or a ribbon button?


Sometimes there are situations where you need to call a plugin from a JavaScript or click of a ribbon button. With the help of actions and latest web API this is easily achievable.

Step 1

Under Processes create a new Process with Category selected as Action; give a name and select the entity. In this example I have selected Account as the entity. It’s just a blank action with no arguments or steps.



Step 2

Create your plugin and register your plugin on that action you created. Make sure you have activated your action and published. Otherwise you will not see the newly created action in the plugin registration tool. If you still don’t see your action name in messages in plugin registration tool close the plugin registration tool and open again. It should resolve this.



Step 3

In your JavaScript on account form use following code; replace the action name with the unique name of your action.
In this example replace new_AccountAction with your unique name.
function CallAction() {   
    var currentRecordIdString = Xrm.Page.data.entity.getId();
    var currentRecordId = currentRecordIdString.replace("{", '').replace("}", '');
    var query = "accounts(" + currentRecordId + ")/Microsoft.Dynamics.CRM.new_AccountAction";
    var req = new XMLHttpRequest();
    var url = Xrm.Page.context.getClientUrl() + "/api/data/v8.0/" + query;
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/" + query, 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.onreadystatechange = function () {
        if (this.readyState == 4) {
            req.onreadystatechange = null;
            if (this.status == 204) {
                //Xrm.Utility.alertDialog("Action Called");
            }
            else {
                var error = JSON.parse(this.response).error;
                Xrm.Utility.alertDialog(error.message);
            }
        }
    };
    req.send();
}


That’s all. Then you can call this function from your ribbon button if you need to.



How to tackle Concurrent Business Process flows

Dynamics 365 has introduced the new feature of Concurrent Business Process Flows. Here is a couple of good articles on that: http://dev...