• Dynamics CRM - 通过 C# Plugin 来 abandon Business Process Flow


    需求说明:

          当一个 Entity 存在 Business Process Process 时,有时我们需要改变其状态,在之前写的博客有讲了可以通过 JavaScript 来实现,本篇就来讲一下如何通过 C# Plugin 来实现对 BPF 的 abandon( abandon 后的 BPF 会变成灰色,BPF 里的  Stages 变成不可编辑,不能点击上一步和下一步,也不能 Set Active;如果想要使 Steps 也不可编辑,可通过 JavaScript 控制)。

    解决方案:

          通过 Solution 查看 Entities 组件时可以发现:在为一个 Entity 添加一个 Business Process Flow 的时候,实际上也是创建了一个新的 Entity(后面简称 BPF Entity),Entity 与 BPF Entity 之间的关系是 1:N。BPF Entity 中存在 Lookup 其 Primary Entity 的 Field,可以通过这个字段来查询当前 Entity 下的 BPF,之后改变其 State 和 Stauts 的值就可以将 BPF abandon 了。

    Note:State -> statecode,Status -> statuscode,这两个字段都是 BPF Entity 的 Default Field,每个 Entity 都有的,表示状态,通过改变这两个字段的值来改变 Entity 的状态。

    示例代码:

    private void AbandonBPF(IOrganizationService service, Guid new_entity_id)
    {
        using (OrganizationServiceContext orgService = new OrganizationServiceContext(service))
        {
            var bpf_entity = (from _bpf_entity in orgService.CreateQuery()
                              where _bpf_entity.bpf_new_entityid.Id == new_entity_id
                              select _bpf_entity).FirstOrDefault();
    
            if (bpf_entity != null && bpf_entity.GetAttributeValue("statecode").Value == 0)
            {
                //statecode = 1 and statuscode = 3 for abandon workflow
                SetStateRequest setStateRequest = new SetStateRequest()
                {
                    EntityMoniker = new EntityReference
                    {
                        Id = bpf_entity.BusinessProcessFlowInstanceId.Value,
                        LogicalName = Entities.new_bpfenity.EntityLogicalName,
                    },
                    State = new OptionSetValue(1),
                    Status = new OptionSetValue(3)
                };
                service.Execute(setStateRequest);
            }
        }
    }

    Note:这里 new_entity 是 Entity name,new_bpfentity 是其对应 BPF Entity 的 name。

    函数调用:

    Entities.new_entity entity =  ((Entity)context.InputParameters["Target"]).ToEntity();
    AbandonBPF(service, entity.id);

  • 相关阅读:
    数仓建设教程
    【Rust日报】2023-09-14 - 推进 `async fn` 稳定化
    开源共建 | TIS整合数据同步工具ChunJun,携手完善开源生态
    SpringBoot+Mybatis 配置多数据源及事务管理
    ★【删除二叉搜索数节点】【递归】Leetcode 450. 删除二叉搜索树中的节点
    Manage SQL Auditing from the Command Line
    VINS学习(一)视觉前端
    leetcode Top100(12)最大子数组和
    闲聊servlet的常见注册方式
    Vatee万腾的数字化掌舵:Vatee科技解决方案的全面引领
  • 原文地址:https://blog.csdn.net/m0_72495985/article/details/127864177