繁体   English   中英

重新打开机会时执行的CRM插件

[英]CRM plugin to execute when opportunity is reopened

我需要为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?

                }
            }
        }
    }

我想你实际上想要在SetStateDynamicEntity消息上为post阶段注册一个插件。 您不希望此消息有任何过滤属性。

您的代码看起来像这样:

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

您将要在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);                      
            }

这是我最终得到的。 我会将此标记为正确的答案,但是当我能够再次投票时,它会给你(Focus和Corey)一个upvote,因为我结合了你们双方的建议来达成这个解决方案。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM