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 one business process to another based on some condition . I can also move to some third process on some other condition. So, I am taking an example in JavaScript in which based on an option set We will be changing Process flow.First we need to get various stage Guids out from CRM . For which, the following code will list out all the Process Id and Stage Id for given business process:
var service = (IOrganizationService)proxy;
QueryExpression query = new QueryExpression
{
EntityName = “workflow”,
ColumnSet = new ColumnSet(true)
};
Console.WriteLine(“Please enter the Process Name: “);
var processName = Console.ReadLine();
query.Criteria.Conditions.Add(new ConditionExpression(“name”, ConditionOperator.Equal, processName));
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);
Console.Clear();
Console.WriteLine(“Process Name: ” + processName);
Console.WriteLine(“Process GUID: ” + response.Entities[0].Id);
foreach (Entity processStage in response1.Entities)
{
Console.WriteLine(processStage.Attributes[“stagename”] + ” : ” + processStage.Attributes[“processstageid”].ToString());
}
If you paste this code to a console application it is going to give you output like this:
Process Name: Opportunity Sales Process 1
Process GUID: a013aa17-3d9e-4bfa-b7f7-72a10817d1c2
close : 0e6bbc59-345c-4cce-8bd0-c63d42b79e34
qualify : 155f3ba4-c9c3-43e4-855d-df97c0bc12ef
propose : 5721bc3b-d2fc-42be-ae37-f332ab3f2eb8
develop : 22e8f4ea-9e9a-4ba3-8c2f-ff15026d7d6f
Now the thing to note here is you need to maintain processid, stageid combination , else CRM will not map the process correctly. In this case I am going to move to Propose stage in Opportunity Sales Process 1 . What you got to be careful is all validations (required fields of previous stages are filled in already by the user. Out of box, CRM would have done it for you but now as you are changing it on the fly, you need to be extra careful here)
On opportunity form I have added an Option set having text option set same as Process Name and values as 1,2 for Process Stage selection. Please refer image below:
Following onchange function in JavaScript for Opportunity Process Selection option set will take care of the rest:
function onchange() {
var selectedProcess = Xrm.Page.getAttribute(“new_processselection”);
var selectedProcessValue = selectedProcess.getValue();
if (selectedProcessValue == 2) {
//Set Guid for Opportunity Sales Prcoess 1
Xrm.Page.getAttribute(“processid”).setSubmitMode(“always”);
Xrm.Page.getAttribute(‘processid’).setValue(‘a013aa17-3d9e-4bfa-b7f7-72a10817d1c2’);
//Set Guid for Opportunity Sales Prcoess 1 Stage Propose
Xrm.Page.getAttribute(“stageid”).setSubmitMode(“always”);
Xrm.Page.getAttribute(‘stageid’).setValue(‘5721bc3b-d2fc-42be-ae37-f332ab3f2eb8’);
//Call Entity save
Xrm.Page.data.entity.save();
//Call window refresh
window.location.reload(true);
}
}
Hope it helps!
Trying to Learn Dynamics CRM: Download!
Learn Dynamics CRM App on Google Play store
