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 = 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!
Above function executes after the stage has been changed. Is it possible to execute javascript function before the change happens.
I will say on change is always triggered after change, how can you trigger before change.
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.
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 ?
I did not quite understand what are you trying to achieve here. Please let me know in more detail.
I thought the same concept when I was looking for trigger a Javacript function when stage changes. It confirmed from your post as well. Good One, it was helpful.