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:

image

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 = Xrm.Page.data.entity.getId();
    //alert(Xrm.Page.getAttribute(‘stageid’).getValue());
    switch (selectedOptionSet.getValue()) {
        case 1:
            //Guid for new stage set here

            Xrm.Page.getAttribute(‘stageid’).setValue(‘3f203c1e-64ae-436e-a41b-464667b57ce8’);
            break;
        case 2:

            //Guid for Open stage set here
            Xrm.Page.getAttribute(‘stageid’).setValue(‘5ecba2d2-4e37-a259-577a-6a0c34b5c537’);
            break;
        case 3:

            //Guid for Close stage set here
            Xrm.Page.getAttribute(‘stageid’).setValue(‘6d49bc00-7343-eead-e2c6-17d28e683219’);
            break;
        default:
            break;
    }

    //Call Entity save
    Xrm.Page.data.entity.save();
}

Hope it helps!

Advertisements

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.

  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:

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s