Microsoft CRM自定义工作流 - 找不到密钥

时间:2013-08-14 10:20:42

标签: c# dynamics-crm-2011

我正在尝试为Microsoft Dynamics CRM 2011编写自定义工作流程作为培训练习。我使用的代码如下所示,适用于标准插件但是,当作为自定义工作流程的一部分运行时,会给我一个字典错误中不存在的键。任何人都可以发现任何原因吗?我已经检查了所有正确的实体和字段名称。

由于

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Collections.ObjectModel;

using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;

using System.Diagnostics;


namespace TestWflow
{

    public class SampleCustomActivity : CodeActivity
    {


        protected override void Execute(CodeActivityContext executionContext)
        {
            //Activity code

            // Get the context service.
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

            // Use the context service to create an instance of IOrganizationService.
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);

            if (context.Depth == 1)
            {

                Entity targetCont = null;
                targetCont = (Entity)context.InputParameters["Target"];
                Guid contID = targetCont.Id;
                ColumnSet contCols = new ColumnSet("jobtitle");

                targetCont = service.Retrieve("contact", contID, contCols);

                targetCont.Attributes["jobtitle"] = "test jobtitle here";
                service.Update(targetCont);


            }



        }



    }


}

1 个答案:

答案 0 :(得分:1)

我的猜测是你的代码爆炸了:     targetCont =(Entity)context.InputParameters [“Target”];

我的插件代码与我的工作流程代码在获取数据方面总是有点不同。

因此,请尝试添加到您的代码中:

context.PrimaryEntityId

最后,您的代码应如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Collections.ObjectModel;

using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;

using System.Diagnostics;


namespace TestWflow
{

    public class SampleCustomActivity : CodeActivity
    {
        protected override void Execute(CodeActivityContext executionContext)
        {
            //Activity code

            // Get the context service.
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>   ();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

            // Use the context service to create an instance of IOrganizationService.
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);

            if (context.Depth == 1)
            {
                Guid contID = context.PrimaryEntityId;
                ColumnSet contCols = new ColumnSet("jobtitle");

                var entity = service.Retrieve("contact", contID, contCols);

                entity.Attributes["jobtitle"] = "test jobtitle here";
                service.Update(entity);


            }



        }



    }


}