【问题标题】:CRM plugin to execute when opportunity is reopened重新打开机会时执行的 CRM 插件
【发布时间】:2011-01-31 22:37:45
【问题描述】:

我需要为 Dynamics CRM 4.0 编写一个插件,该插件在关闭的机会重新打开时执行,以更改 salesstagecode。我的问题是:

  • 当我向插件注册新步骤时,我应该过滤哪些属性?
  • 我应该检查实体上的哪些属性的值?和
  • 我应该寻找这个实体的值,以便确定插件执行是否应该继续?

我通常编写异步工作流,并且我编写插件的经验仍在开发中,因此我将不胜感激任何可以提供的帮助和说明。

请看下面我写的插件骨架

    public void Execute(IPluginExecutionContext context)
    {
        if (context.InputParameters.Properties.Contains("Target") && context.InputParameters.Properties["Target"] is DynamicEntity)
        {
            ICrmService service = context.CreateCrmService(false);

            DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties["Target"];

            if (entity.Name == EntityName.opportunity.ToString())
            {
                if (entity.Properties.Contains(/*What Property Should I Check Here?*/))
                {
                    //And what value should I be looking for in that property?

                }
            }
        }
    }

【问题讨论】:

    标签: c# .net workflow-foundation dynamics-crm dynamics-crm-4


    【解决方案1】:

    这是我最终得出的结论。我会将其标记为正确答案,但当我能够再次投票时,我会为你们(Focus 和 Corey)投票,因为我采纳了你们两人的建议以达成此解决方案。

        public void Execute(IPluginExecutionContext context)
        {
            if (context.InputParameters.Properties.Contains("Target") &&
                context.InputParameters.Properties["Target"] is DynamicEntity)
            {
                DynamicEntity opp = (DynamicEntity)context.InputParameters["Target"];
                Picklist StageCodePicklist = new Picklist(); (Picklist);opp.Properties["salesstagecode"];
                StageCodePicklist.name = "Advocating - Advanced (90%)";
                StageCodePicklist.Value = 200004;
                opp.Properties["salesstagecode"] = StageCodePicklist;
            }
        }
    

    【讨论】:

      【解决方案2】:

      我认为您实际上希望在 SetStateDynamicEntity 消息上为后期阶段注册一个插件。您不需要对此消息的任何过滤属性。

      您的代码将如下所示:

      public void Execute(IPluginExecutionContext context)
      {
          string state = (string)context.InputParameters["State"];
          if (state == "Open")
          {
              Moniker entityMoniker = (Moniker)context.InputParameters["EntityMoniker"];
              DynamicEntity opp = new DynamicEntity("opportunity");
              opp["opportunityid"] = new Key(entityMoniker.Id);
              opp["salesstagecode"] = new Picklist(/*your value*/);
              context.CreateCrmService(true).Update(opp);
          }
      }
      

      【讨论】:

      • Microsoft 建议针对 SetState 和 SetStateDynamicEntity 消息双重注册任何内容,因为您无法始终确定哪一个会被解雇。
      【解决方案3】:

      您将要在 SetStateDynamic 消息上设置实体。这不提供目标,因此您需要提取 EntityMoniker 并手动检索实体,如下所示:

                  // If this is a setstate call, we need to manually pull the entity
                  if (context.InputParameters.Properties.Contains("EntityMoniker") &&
                          context.InputParameters.Properties["EntityMoniker"] is Moniker)
                  {
                      Moniker entity = (Moniker)context.InputParameters.Properties["EntityMoniker"];
      
                      // get the entity
                      TargetRetrieveDynamic targetRet = new TargetRetrieveDynamic();
                      targetRet.EntityId = entity.Id;
                      targetRet.EntityName = context.PrimaryEntityName;
      
                      RetrieveRequest retrieveReq = new RetrieveRequest();
                      retrieveReq.ColumnSet = new ColumnSet();
                      retrieveReq.ColumnSet.AddColumns(new string[]{"opportunityid", "statecode", "statuscode"});
                      retrieveReq.Target = targetRet;
                      retrieveReq.ReturnDynamicEntities = true;
      
                      RetrieveResponse retrieveRes = this.Service.Execute(retrieveReq) as RetrieveResponse;
      
                      // Set the new entity and the status
                      int status = (int)context.InputParameters["Status"];
                      dynEntity = (DynamicEntity)retrieveRes.BusinessEntity;                                        
                      dynEntity.Properties["statuscode"] = new Status(status);                      
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-27
        • 1970-01-01
        • 1970-01-01
        • 2019-08-21
        • 1970-01-01
        • 1970-01-01
        • 2014-08-15
        相关资源
        最近更新 更多