How can i Change business process flow programatically(c#) in CRM 365. Any code that helps
*This post is locked for comments
How can i Change business process flow programatically(c#) in CRM 365. Any code that helps
*This post is locked for comments
There is an SDK request you can use to switch processes:
msdn.microsoft.com/.../microsoft.crm.sdk.messages.setprocessrequest.aspx
It works in 365, but you have to run it for each user (since you have to switch the process not just for the record, but, also, for the user now). If you do it in a plugin, it only worked for me in "pre-validate" (and, then, I would impersonate the OrgService for different users.. one after another)
As for jumping the stages etc - did not try that
You can check it out as you have control of setting the Stage. Ideally BPFs are designed to follow Stage by Stage, if you have requirement to skip some of the Stage, I would suggest to create a new BPF and depending upon your condition change BPF which has less Stages
Make sure to Vote as Helpful and Mark As Answer ,if you get answer of your question
Hi Nitya, First link is not working and second link is for older versions. It wont work on 365 version
Thanks for sharing the template. Suppose i have five stages (1,2,3,4,5). The active stage is 2. Can i modify the above code to jump from 2 -->5 ? Is that feature available through program?
Hi,
In Dynamics 365 when you create BPF, a new Entity will be created where your Process/Stage are maintained. Recently I have successfully completed, below is my code, but you should be able to tweak according to your requirement
I have custom Entity called "wbg_concept" and its corresponding BPF Entity is "wbg_conceptbusinessprocessflow". I have written a custom workflow activity to fire on status reason change and then update BPF Entity, so that stage gets automatically updated on my custom Entity. Also check the input parameters which I am passing to custom workflow activity. Copy below code to an editor like VS, so that you can understand the code easily
// <copyright file="WorkFlowActivityBase.cs" company="">
// Copyright (c) 2017 All Rights Reserved
// </copyright>
// <author></author>
// <date>5/19/2017 11:08:02 AM</date>
// <summary>Implements the WorkFlowActivityBase Workflow Activity.</summary>
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.1
// </auto-generated>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading.Tasks;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System.Runtime.Serialization;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
namespace UpdateBizProcessStage {
public partial class UpdateBizProcessStageActivity : CodeActivity {
protected override void Execute(CodeActivityContext executionContext) {
if (executionContext == null) {
throw new ArgumentNullException("serviceProvider");
}
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId);
try {
#region Local Variables
Guid processguid = Guid.Empty;
string stagename = string.Empty;
Guid activestageguid = Guid.Empty;
Guid conceptguid = Guid.Empty;
Guid conceptbpfinstanceguid = Guid.Empty;
int status = 0;
int statusreason = 1;
#endregion
// Get the value from Input Parameters
processguid = ProcessId.Get<EntityReference>(executionContext)!=null ? ProcessId.Get<EntityReference>(executionContext).Id : Guid.Empty;
activestageguid = ActivestageId.Get<EntityReference>(executionContext)!=null ? ActivestageId.Get<EntityReference>(executionContext).Id : Guid.Empty;
status = Status.Get<int>(executionContext);
statusreason = StatusReason.Get<int>(executionContext);
conceptguid = context.PrimaryEntityId;
//Retrieve Concept BPF Instance Guid
QueryExpression bpfinstancequery = new QueryExpression("wbg_conceptbusinessprocessflow");
ColumnSet bpfinstancequeryColSet = new ColumnSet("businessprocessflowinstanceid");
bpfinstancequery.ColumnSet = bpfinstancequeryColSet;
bpfinstancequery.Criteria = new FilterExpression();
bpfinstancequery.Criteria.FilterOperator = LogicalOperator.And;
bpfinstancequery.Criteria.AddCondition("wbg_wbg_conceptid", ConditionOperator.Equal, conceptguid);
EntityCollection bpfinstancecoll = orgService.RetrieveMultiple(bpfinstancequery);
foreach (Entity bpfinstance in bpfinstancecoll.Entities) {
conceptbpfinstanceguid = bpfinstance.Id;
}
if (conceptbpfinstanceguid != Guid.Empty) {
if (activestageguid != Guid.Empty) {
if (activestageguid == new Guid("08af09a2-0c6c-41c9-95cf-4ca0eb24d51c"))
{
SetStateRequest setState = new SetStateRequest();
setState.EntityMoniker = new EntityReference("wbg_conceptbusinessprocessflow", conceptbpfinstanceguid);
setState.State = new OptionSetValue(0);
setState.Status = new OptionSetValue(1);
SetStateResponse setStateResponse = (SetStateResponse)orgService.Execute(setState);
Entity bpfinstance = new Entity();
bpfinstance.LogicalName = "wbg_conceptbusinessprocessflow";
bpfinstance.Id = conceptbpfinstanceguid;
if (processguid != Guid.Empty) {
bpfinstance.Attributes["processid"] = new EntityReference("workflow", processguid);
}
if (activestageguid != Guid.Empty) {
bpfinstance.Attributes["activestageid"] = new EntityReference("processstage", activestageguid);
}
//bpfinstance.Attributes["statecode"] = new OptionSetValue(status);
//bpfinstance.Attributes["statuscode"] = new OptionSetValue(statusreason);
orgService.Update(bpfinstance);
} else {
Entity bpfinstance = new Entity();
bpfinstance.LogicalName = "wbg_conceptbusinessprocessflow";
bpfinstance.Id = conceptbpfinstanceguid;
if (processguid != Guid.Empty) {
bpfinstance.Attributes["processid"] = new EntityReference("workflow", processguid);
}
if (activestageguid != Guid.Empty) {
bpfinstance.Attributes["activestageid"] = new EntityReference("processstage", activestageguid);
}
bpfinstance.Attributes["statecode"] = new OptionSetValue(status);
bpfinstance.Attributes["statuscode"] = new OptionSetValue(statusreason);
orgService.Update(bpfinstance);
}
} else {
Entity bpfinstance = new Entity();
bpfinstance.LogicalName = "wbg_conceptbusinessprocessflow";
bpfinstance.Id = conceptbpfinstanceguid;
if (processguid != Guid.Empty) {
bpfinstance.Attributes["processid"] = new EntityReference("workflow", processguid);
}
if (activestageguid != Guid.Empty) {
bpfinstance.Attributes["activestageid"] = new EntityReference("processstage", activestageguid);
}
bpfinstance.Attributes["statecode"] = new OptionSetValue(status);
bpfinstance.Attributes["statuscode"] = new OptionSetValue(statusreason);
orgService.Update(bpfinstance);
}
}
} catch (FaultException<OrganizationServiceFault> e) {
// Handle the exception.
throw;
} finally {
//localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exiting {0}.Execute()", this.ChildClassName));
}
}
/// Gets or sets Is Contact Available for given UPI
/// </summary>
[Input("Process")]
[ReferenceTarget("workflow")]
public InArgument<EntityReference> ProcessId { get; set; }
[Input("Stage")]
[ReferenceTarget("processstage")]
public InArgument<EntityReference> ActivestageId { get; set; }
[RequiredArgument]
[Input("Status")]
[Default("0")]
public InArgument<int> Status { get; set; }
[RequiredArgument]
[Input("Status Reason")]
[Default("1")]
public InArgument<int> StatusReason { get; set; }
}
}
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,269 Super User 2024 Season 2
Martin Dráb 230,198 Most Valuable Professional
nmaenpaa 101,156