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!