CRM - Workflow Activity - Error: The given key was not present in the dictionary.

CRM - Workflow Activity - Error: The given key was not present in the dictionary.

我正在执行来自 CRM 流程的工作流 Activity。 我有两个输入,它们是 EntityReference 并且正在填充过程中。 不要打印 try 之后的痕迹。只需输入捕获。我不知道为什么。 我的代码是:

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

    }

}

谢谢。

您尝试访问的选项集值 "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)
{
  ....
}