Change Business Process Flow stages using JavaScript

Out of box Business Process Flows in CRM 2013 will only move in one direction , i.e. from first stage to second till last. But , real world scenarios are different. We want to create Business Process flows with conditional formatting. From first stage i can move to second stage on some condition . I can also move to some third stage on some other condition. So, I am taking an example in JavaScript in which based on an option set We will be changing Stage for business process flow.First we need to get various stage Guids out from CRM . For which, the following code will list out all the Stage Id for given business process:

             var service = (IOrganizationService)proxy;
                  QueryExpression query = new QueryExpression
                    EntityName = “workflow”,
                    ColumnSet = new ColumnSet(true)

                  query.Criteria.Conditions.Add(new ConditionExpression(“name”, ConditionOperator.Equal, “Business Process Flow name here”));
                EntityCollection response = service.RetrieveMultiple(query);

                QueryExpression query1 = new QueryExpression
                    EntityName = “processstage”,
                    ColumnSet = new ColumnSet(true)

                query1.Criteria.Conditions.Add(new ConditionExpression(“processid”, ConditionOperator.Equal, response.Entities[0].Id));
                EntityCollection response1 = service.RetrieveMultiple(query1);

In response1 collection you will find stage name and guid which I will use on Opportunity form .On opportunity form I have added an Option set having text option set same as Stage and values as 1,2 and 3. Please refer image below:


Following onchange function in JavaScript for Opportunity Stage option set will take care of the rest:

function onChange() {

    var selectedOptionSet = Xrm.Page.getAttribute(“new_opportunitystage”);
    //var Id =;
    switch (selectedOptionSet.getValue()) {
        case 1:
            //Guid for new stage set here

        case 2:

            //Guid for Open stage set here
        case 3:

            //Guid for Close stage set here

    //Call Entity save;

Hope it helps!


7 thoughts on “Change Business Process Flow stages using JavaScript

  1. Above function executes after the stage has been changed. Is it possible to execute javascript function before the change happens.

      • The reason i want to execute the javascript before the change is, i dont want the user to move the stage, if certain condition is not satisfied.

      • Hi Ravi,

        so only execute the stage change code when conditions are satisfied. else do not move the stage.

        Else if the conditions are not satisfied update stage to current one only back, in case user has moved by same scripting.

  2. How do i stop stage move using javascript when i click on Next Stage button. I’ve used return false from the javascript function that executes when the Next Stage button is clicked but i dont think it seems to be working. Any ideas ?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s