【问题标题】:CRM - Workflow Activity - Error: The given key was not present in the dictionary.CRM - 工作流活动 - 错误:字典中不存在给定的键。
【发布时间】:2016-12-18 10:47:29
【问题描述】:

我正在执行来自 CRM 流程的工作流活动。 我有两个输入,它们是 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));
        }

    }

}

谢谢。

【问题讨论】:

    标签: c# dynamics-crm


    【解决方案1】:

    您尝试访问“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)
    {
      ....
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多