kivikall
kivikall

Reputation: 795

MS Dynamics CE CRM 365 - Pre Operation Plugin - Given key was not present in the dictionary

After few years doing some other stuff I'm back to CRM business. I'm already questioning my life choices. I don't understand what is wrong here. I'm trying to create a simple plugin that will run on Incident / Case creation. It will look if description field contains a valid url and if yes then it should update first url that has been found to another field. Here's the plugin execution method.

    public void Execute(IServiceProvider serviceProvider)
    {
        ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
        IPluginExecutionContext pluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

        if (pluginExecutionContext.InputParameters.Contains("Target") && pluginExecutionContext.InputParameters["Target"] is Entity)
        {
            Entity targetEntity = (Entity)pluginExecutionContext.InputParameters["Target"];
            if (targetEntity.LogicalName != Incident.EntityLogicalName)
            {
                return;
            }

            IOrganizationServiceFactory orgServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService orgService = orgServiceFactory.CreateOrganizationService(pluginExecutionContext.UserId);

            try
            {
                string desc = (string)targetEntity.Attributes["description"];
                string pattern = @"\b(?:https?://|www\.)[^ \f\n\r\t\v\]]+\b";

                MatchCollection collection = Regex.Matches(desc, pattern);
                if (collection.Count > 0)
                {
                    throw new Exception(collection[0].Value);
                }
            }
            catch (Exception ex)
            {
                tracingService.Trace("Error in CaseUrlPlugin {0}", ex.ToString());
                throw ex;
            }
        }
    }

The problem is that when I'm creating a new case (description field filled with text and url) and hit save I get "Given key was not present in the dictionary" exception as if description field is not there. When I hit ok to that error window and hit save again then description field is founded and my code throws an exception with that link.

So why is not the description field present in the first time? I don't like the idea doing this post operation because that would require another sql transaction right (to save incident again)?

Upvotes: 0

Views: 405

Answers (1)

kivikall
kivikall

Reputation: 795

Aah ffs. The problem was that when I was trying to save for the first time I used CTRL + S while my focus was still on description field because that's the last field I was filling. Now it seems that UI doesn't register that field to be filled if focus on that field and hitting CTRL + S. Hitting save icon works because that would get focus off the description field. And of course in my case hitting ok for that error window also unfocus the field thus second save works.............

Well, at least I figured it out right after posting question here. Been trying to solve this way too long.

Upvotes: 1

Related Questions