{Dynamics CRM} Control CRM 2013 Business Process Next Stage and Previous Stage flow using JScript.

Recently in our project we needed to restrict the business process flow from one stage to another depending on certain conditions. The problem was that OOB CRM 2013 does allow you to plugin your custom client side code when the “Next Stage” or the “Previous Stage” icons are clicked. You can obviously register Plugins and Workflows on the business process stage change but you cannot stop the change of stages using the same. Let’s see how we can achieve this.
Below is the screenshot of an example business process flow for opportunity entity. Let’s explore what CRM does here. I have opened Chrome Developer tools and trying to explore the HTML for business process flow. I have selected the ‘Next Stage’ button.
screen 9
The id of the div is “stageAdvanceActionContainer”.
So what we need to do is override the click event with our custom function and then if our condition meets we would call the OOB event handler. The code below does the same.
function  registerBusinessProcessEvents()
{
var originalNextStageHandler = $(“#stageAdvanceActionContainer”).data(“events”)[“click”][0].handler;
 
// unbind the original next event handler
$(“#stageAdvanceActionContainer”).unbind(“click”);
 
$(“#stageAdvanceActionContainer”).click(function (e) {
// perform your custom logic here. If you want to move to the next stage just call the code below.
 
$.proxy(originalNextStageHandler, $(“#stageAdvanceActionContainer”))(e);
 
}
 
}
All you need to do is call the registerBusinessProcessEvents  function during the onload of the form.
Similarly the ID for the previous click handler is “stageBackActionContainer”. And you can handle it in the same manner as above.
 
Please note that is an unsupported customization and might not work if Microsoft decides to change the id of the div elements or the rendering model in future rollups.
 
Hope this helps!

11 thoughts on “{Dynamics CRM} Control CRM 2013 Business Process Next Stage and Previous Stage flow using JScript.”

  1. I am unable to capture the default handler in MS CRM 2015 (below is the code), will there be any other approach?
    var originalNextStageHandler = $._data($(“#stageAdvanceActionContainer”)[0], “events”);

      1. You are right there are supported customizations to handle process stage events. But OnStageChange event will fire after the stage change but I need to handle few validation before the stage change. Is there any way to handle this using OOB?

  2. Hi,
    I want to prevent opening a form on double clicking the record in a subgrid in ms crm 2011, can you please tell how to achieve this?
    How to interact with the oob event handler..any idea?

    1. Hi Pavan,
      Thanks for reading my post.
      Would like to know here the requirement like why you want to block the the double click of the row of the subgrid? Perhaps we can have an alternative solution here.

      1. Hi Debajit,
        Sorry for my late response.I want users not to open the activity record in activity subgrid in a custom entity,which is of particular subject. But all users must be able to see all the records in all views…
        Only the owner can open the activity record and rest are restricted ….so i am checking for preventing form onload(oob)…Can you think on this tell me any other possible solution….
        Thanks,
        Pavan

        1. Hi Pavan,
          Since all users can see the activities the in the views, is there any harm if they can still open the activity and see it but only in read-only mode so that they cannot modify it?

  3. Pingback: Registering custom client handlers for your business process flow stage handlers –Dynamics CRM | Debajit's Dynamic CRM Blog

  4. Pingback: Debajit's Dynamics CRM Blog

Comments are closed.