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

发布于 2024-08-26 18:49:21 字数 986 浏览 10 评论 0原文

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

                }
            }
        }
    }

I need to write a plugin for Dynamics CRM 4.0 that executes when a closed opportunity is reopened in order to change the salesstagecode. My questions are:

  • When I register a new step to the plugin, what attribute(s) should I filter on?
  • What property on the entity should I check the value of? and
  • What should I look for the value of this entity to be so I can determine if the plugin execution should continue?

I've typically written asynchronous workflows and my experience writing plugins is still developing, so I'd appreciate any help and clarification that can be offered.

Please see below the plugin skeleton I've written

    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?

                }
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热鲨 2024-09-02 18:49:21

我认为您实际上想要在 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);
    }
}

I think you'll actually want to register a plugin for the post stage on the SetStateDynamicEntity message. You won't want any filtering attributes for this message.

Your code would look something like this:

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);
    }
}
花落人断肠 2024-09-02 18:49:21

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

You're going to want to set up the entity on the SetStateDynamic message. This doesn't provide a target, so instead you'll need to pull the EntityMoniker and manually retrieve the entity like this:

            // 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);                      
            }
忘羡 2024-09-02 18:49:21

这就是我最终得出的结论。我会将其标记为正确答案,但当我能够再次投票时,我会给你们俩(焦点和科里)投票,因为我采纳了你们俩的建议来达成这个解决方案。

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

Here is what I ultimately arrived at. I will mark this as the correct answer but will give you both (Focus and Corey) an upvote when I'm able to upvote again because I incorporated suggestions from both of you to arrive at this solution.

    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;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文