Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Business process flow 365

Posted on by Microsoft Employee

How can i Change business process flow programatically(c#) in CRM 365. Any code that helps

*This post is locked for comments

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Business process flow 365

    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

  • Shaik Profile Picture
    Shaik on at
    RE: Business process flow 365

    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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Business process flow 365

    Hi Nitya, First link is not working and second link is for older versions. It wont work on 365 version

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Business process flow 365

    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?

  • Suggested answer
    Shaik Profile Picture
    Shaik on at
    RE: Business process flow 365

    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; }

    }
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans