C# CRM-工作流活动-错误:字典中不存在给定的密钥。

C# CRM-工作流活动-错误:字典中不存在给定的密钥。,c#,dynamics-crm,C#,Dynamics Crm,我正在做一个来自CRM流程的工作流活动。 我有两个输入,它们是EntityReference,并在流程中填充。 不要打印尝试后的跟踪。只需输入捕获。我不知道为什么。 我的代码是: public class WK_DecorrerObjetivo : CodeActivity { //inputs dialog --- // input alvo [Input("Alvo")] [ReferenceTarget("xpto_alvo")] public InArg

我正在做一个来自CRM流程的工作流活动。 我有两个输入,它们是EntityReference,并在流程中填充。 不要打印尝试后的跟踪。只需输入捕获。我不知道为什么。 我的代码是:

 public class WK_DecorrerObjetivo : CodeActivity
{
    //inputs dialog --- // input alvo
    [Input("Alvo")]
    [ReferenceTarget("xpto_alvo")]
    public InArgument<EntityReference> alvo { get; set; }

    //input actividade xpto_atividadeobjetivoid
    [Input("Actividade Objetivo")]
    [ReferenceTarget("xpto_atividadedeobjetivo")]
    public InArgument<EntityReference> atividadeObjetivo { get; set; }

    protected override void Execute(CodeActivityContext Execontext)
    {

        ITracingService _tracing;
        IWorkflowContext context = null;
        IOrganizationServiceFactory serviceFactory = null;
        IOrganizationService service = null;  
        OrganizationServiceContext serviceContext = null;

        try
        {
            #region Get Work Flow Context

            context = Execontext.GetExtension<IWorkflowContext>();
            serviceFactory = Execontext.GetExtension<IOrganizationServiceFactory>();
            service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);
            serviceContext = new OrganizationServiceContext(service);

            _tracing = Execontext.GetExtension<ITracingService>();
            _tracing.Trace("inicio do try");

            FetchExpression query = new FetchExpression(string.Format(Resources.GetTemplateAtividade, context.PrimaryEntityId));

            // Obtain result from the query expression.
            Entity new_alvo = (Entity)context.InputParameters["Target"];
            var alvoGUID = ((EntityReference)new_alvo["xpto_alvo"]).Id;
            Entity retrieveTemp = service.Retrieve("xpto_alvo", ((EntityReference)new_alvo["xpto_alvo"]).Id, new ColumnSet("xpto_utilizador", "xpto_conta", "xpto_contacto", "xpto_alvoid", "xpto_name", "createdon", "xpto_estado", "xpto_resultadoimportacao", "xpto_objetivoassociadoid", "xpto_alvo"));

            OptionSetValue tipoAtividade = (OptionSetValue)retrieveTemp.Attributes["xpto_tipoatividade"];
                switch (tipoAtividade.Value)
                {
                case 0:
                    _tracing.Trace("entrou no case 0 - compromisso");

                    break;

                case 1:
                    _tracing.Trace("entrou no case 1 - phonecall");

                    break;

                case 2:
                    _tracing.Trace("entrou no case 2 - task");
                    break;

                default:
                    break;
            }
            //serviceContext.SaveChanges(); _tracing.Trace("savechanges");
        }

        catch (Exception ex)
        {
            string msgErro;

            if (ex.InnerException != null)
            {
                msgErro = ex.InnerException.Message;
            }
            else
            {
                msgErro = ex.Message;
            }

            throw new InvalidPluginExecutionException(string.Format("Erro ao decorrer objetivo: {0}", msgErro));
        }

    }

}
public class WK\u decorrelrobjetivo:code活动
{
//输入对话框--//输入alvo
[输入(“Alvo”)]
[参考目标(“xpto_alvo”)]
公共不规则alvo{get;set;}
//输入活动xpto_atividadeObjectIvoid
[输入(“活动对象”)]
[参考目标(“xpto_atividedeobjetivo”)]
公共InArgument atividadeObjetivo{get;set;}
受保护的重写无效执行(CodeActivityContext Execontext)
{
ITracingService\u跟踪;
IWorkflowContext上下文=null;
IOOrganizationServiceFactory服务工厂=null;
IOOrganizationService=null;
OrganizationServiceContext serviceContext=null;
尝试
{
#区域获取工作流上下文
context=Execontext.GetExtension();
serviceFactory=Execontext.GetExtension();
服务=serviceFactory.CreateOrganizationService(context.InitiatingUserId);
serviceContext=新组织serviceContext(服务);
_tracing=Execontext.GetExtension();
_tracing.Trace(“inicio do try”);
FetchExpression query=新的FetchExpression(string.Format(Resources.getTemplateIVIDade,context.PrimaryEntityId));
//从查询表达式中获取结果。
实体new_alvo=(实体)context.InputParameters[“Target”];
var alvoGUID=((EntityReference)new_alvo[“xpto_alvo]”)Id;
实体检索temp=service.Retrieve(“xpto\u alvo”,((实体引用)new\u alvo[“xpto\u alvo”).Id,新列集(“xpto\u utilizador”、“xpto\u conta”、“xpto\u contact”、“xpto\u alvoid”、“xpto\u name”、“createdon”、“xpto\u estado”、“xpto\u resultadoimportaco”、“xpto\u objetivoassociadoid”、“xpto\u alvo”);
OptionSetValue tipoAtividade=(OptionSetValue)retrieveTemp.Attributes[“xpto_tipoAtividade”];
开关(tipoAtividade.值)
{
案例0:
_tracing.Trace(“entrou无案例0-妥协”);
打破
案例1:
_Trace.Trace(“entrou no case 1-phonecall”);
打破
案例2:
_Trace.Trace(“entrou无案例2-任务”);
打破
违约:
打破
}
//serviceContext.SaveChanges();_tracing.Trace(“SaveChanges”);
}
捕获(例外情况除外)
{
字符串msgErro;
if(例如InnerException!=null)
{
msgErro=ex.InnerException.Message;
}
其他的
{
msgErro=例如消息;
}
抛出新的InvalidPluginExecutionException(string.Format(“Erro ao decorre objetivo:{0}”,msgErro));
}
}
}

谢谢。

您尝试访问的选项集值“xpto\u tipoatividade”不包括在要在检索请求中获取的列列表中。还要始终检查返回的实体属性集合中是否存在该属性,并对其进行安全强制转换,或使用扩展方法获取默认值

var retrieveTemp = service.Retrieve("xpto_alvo",
                   ((EntityReference) new_alvo["xpto_alvo"]).Id,
                   new ColumnSet(
                   "xpto_tipoatividade", //<-- add xpto_tipoatividade to the list of attributes to fetch
                   "xpto_utilizador",
                   "xpto_conta",
                   "xpto_contacto",
                   "xpto_alvoid",
                   "xpto_name",
                   "createdon",
                   "xpto_estado",
                   "xpto_resultadoimportacao",
                   "xpto_objetivoassociadoid",
                   "xpto_alvo"));

var tipoAtividade = retrieveTemp.GetAttributeValue<OptionSetValue>("xpto_tipoatividade");

if (tipoAtividade == null)
{
   _tracing.Trace("tipoAtividade is null, returning");
   return;
}
switch (tipoAtividade.Value)
{
  ....
}
var retrieveTemp=service.Retrieve(“xpto_alvo”,
((实体参考)新阿尔沃[“xpto阿尔沃]).Id,
新列集(

“xpto_tipoatividade”,//您尝试访问的选项集值“xpto_tipoatividade”不包括在要在检索请求中获取的列列表中。另外,始终检查返回的实体属性集合中是否存在该属性,并将其安全转换或使用扩展方法获取默认值

var retrieveTemp = service.Retrieve("xpto_alvo",
                   ((EntityReference) new_alvo["xpto_alvo"]).Id,
                   new ColumnSet(
                   "xpto_tipoatividade", //<-- add xpto_tipoatividade to the list of attributes to fetch
                   "xpto_utilizador",
                   "xpto_conta",
                   "xpto_contacto",
                   "xpto_alvoid",
                   "xpto_name",
                   "createdon",
                   "xpto_estado",
                   "xpto_resultadoimportacao",
                   "xpto_objetivoassociadoid",
                   "xpto_alvo"));

var tipoAtividade = retrieveTemp.GetAttributeValue<OptionSetValue>("xpto_tipoatividade");

if (tipoAtividade == null)
{
   _tracing.Trace("tipoAtividade is null, returning");
   return;
}
switch (tipoAtividade.Value)
{
  ....
}
var retrieveTemp=service.Retrieve(“xpto_alvo”,
((实体参考)新阿尔沃[“xpto阿尔沃]).Id,
新列集(
“xpto_tipoatividade”//