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!

Business Process Flow in CRM 2013: Basics

As per CRM SDK, A business process flow guides you through various stages in the business process, from start to finish. Each stage contains predefined, logically connected steps. Your paradigm changes from form-centric to processes-centric. You no longer need to remember all the forms you have to use and in which order. The process flow will tell you where you are in the process, where you came from, and what to do next. You are able to continuously monitor your own progress.

We will go through the steps required to create a Business Process Flow for Opportunity entity which are as follows:

1. Go to Settings –> Process. Click on new Process and select category as Business Process Flow. Fill in the details as per below

image and Click OK:

image

2.  A screen as below will appear in which we can specify various stages of Business Process Flow. Start editing by giving a new name for Stage (where marked as New Stage in the screen):

image

3. Start filling the Business Process stage as per below screen and mark required field as per below screens. Use + button next to Steps to add and pick attributes that you want to fill as part of the stage. Mark once required in the check box next to attribute:

image

4. Add multiple stages using the following button:

image

5. We will add New,Open and Closed stage for Opportunity:

image

6. Save and Close this Business Process and Activate it. It will be available in the Opportunity form now:

image

I will cover more on CRM 2013 Business Process and other features in coming posts.