简体   繁体   中英

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

I'm doing a Workflow Activity that comes from a CRM process . I have two inputs that are EntityReference and are being filled in the process. Don't printing the trace that is after the try . Just enter the catch. And i doný know why. My code is:

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

    }

}

Thank's.

The option set value you are trying to access "xpto_tipoatividade" isn't included in the list of columns to fetch in the retrieve request. Also always check if the attribute exists in the returned entity attribute collection and safe cast it or use an extension method to get a default value.

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)
{
  ....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM