locked
Problem When Using Pre/Post Entity Images In Plugin RRS feed

  • Question

  • Hi,

    I'm running Dynamics CRM Online 2015 and I'm trying to create a plugin that runs on Update of my custom entity (grm_contract.)

    My code is failing with the message "Unexpected exception from plug-in (Execute): GRMPlugins.OnContractUpdate: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary."

    It stops at the trace "Getting image entity from PostEntityImages."

    I'm running the plugin on update of grm_contract as a post operation (synchronous.)

    using System;
    using System.ServiceModel;
    using System.Runtime.Serialization;
    using Microsoft.Xrm.Sdk;
    
    namespace GRMPlugins
    {
        public class OnContractUpdate : IPlugin
        {
    
            public void Execute(IServiceProvider serviceProvider)
            {
    
                //Extract the tracing service for use in debugging sandboxed plug-ins.
                ITracingService tracingService =
                    (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)
                    serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                // Get a reference to the Organization service.
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                {
    
                    // Obtain the target entity from the Input Parameters.
                    tracingService.Trace
                        ("GRMContractPlugin: Getting the target entity from Input Parameters.");
                    Entity entity = (Entity)context.InputParameters["Target"];
    
                    // Obtain the image entity from the Post Entity Images.
                    tracingService.Trace
                        ("GRMContractPlugin: Getting image entity from PostEntityImages.");
                    Entity image = (Entity)context.PostEntityImages["Target"];
    
                    // Verify that the target entity represents a grm_contract.
                    // If not, this plug-in was not registered correctly.
                    tracingService.Trace
                        ("GRMContractPlugin: Verifying that the target entity represents a grm_contract.");
                    if (entity.LogicalName != "grm_contract")
                        return;
    
                    // Calculate and set grm_contract_end_date if grm_contractperiodvalue, grm_contractperiodunit and grm_contract_start_date ALL exist.
                    if (image.Attributes.Contains("grm_contractperiodvalue") && image.Attributes.Contains("grm_contractperiodunit") && image.Attributes.Contains("grm_contract_start_date"))
                    {
    
                        int contract_period_value = ((int)image.Attributes["grm_contractperiodvalue"]); // Contract period value (numeric) as integer (again)
                        int contract_period_unit_int = ((OptionSetValue)image["grm_contractperiodunit"]).Value; // Get contract period unit as integer so we can use if statements below.
                        DateTime contract_start_date = (DateTime)image.Attributes["grm_contract_start_date"];
    
                        // If grm_contractperiodunit is set to 172300000 (Day)
                        if (contract_period_unit_int == 172300000)
                        {
                            int days_to_add = contract_period_value;
                            DateTime contract_end_date = (DateTime)contract_start_date.AddDays(days_to_add);
                            entity.Attributes.Add("grm_contract_end_date", (DateTime)contract_end_date);
                        }
    
                        // If grm_contractperiodunit is set to 172300001 (Week)
                        if (contract_period_unit_int == 172300001)
                        {
                            int days_to_add = contract_period_value;
                            DateTime contract_end_date = (DateTime)contract_start_date.AddDays(days_to_add * 7); // Have to multiply the days by 7 as there is no AddWeeks function here.
                            entity.Attributes.Add("grm_contract_end_date", (DateTime)contract_end_date);
                        }
    
                        // If grm_contractperiodunit is set to 172300002 (Month)
                        if (contract_period_unit_int == 172300002)
                        {
                            int months_to_add = contract_period_value;
                            DateTime contract_end_date = (DateTime)contract_start_date.AddMonths(months_to_add);
                            entity.Attributes.Add("grm_contract_end_date", (DateTime)contract_end_date);
    
                        }
    
                        // If grm_contractperiodunit is set to 172300003 (Year)
                        if (contract_period_unit_int == 172300003)
                        {
                            int years_to_add = contract_period_value;
                            DateTime contract_end_date = (DateTime)contract_start_date.AddYears(years_to_add);
                            entity.Attributes.Add("grm_contract_end_date", (DateTime)contract_end_date);
                        }
    
                    }
    
                }
    
            }
        }
    
    }
    


    I think the real cause of this problem is the little knowledge I have of how plugins work in Dynamics CRM. If somebody could take a look at my code and identify any areas that don't make sense and could be causing the issue, that would be great.

    Thanks,

    James

    Thursday, October 15, 2015 11:53 AM

Answers

  • Hello James,

    Please provide screenshots of registration of image for a plugin like following screenshots:


    Dynamics CRM MVP
    My blog

    • Marked as answer by JamesGRM Thursday, October 15, 2015 1:27 PM
    Thursday, October 15, 2015 1:07 PM
    Moderator

All replies

  • Hello James,

    I believe your plugin fails on line

    Entity image = (Entity)context.PostEntityImages["Target"];

    Are you sure that you've named Image with "Target" name?

    Anyway the best way to troubleshoot plugin is to use Plugin registration Tool + VS. Recheck this video that describes troubleshooting - https://www.youtube.com/watch?v=Ife_rHq-wSo


    Dynamics CRM MVP
    My blog

    Thursday, October 15, 2015 12:37 PM
    Moderator
  • Hi Andrii,

    Thanks for that. As I say I'm not that familiar with programming in C#. I don't understand what you mean by have i named image with "target"? I'm just using samples I find online to attempt to build my plugins.

    How would I name image with "Target"?

    Thursday, October 15, 2015 12:43 PM
  • Hello James,

    Please provide screenshots of registration of image for a plugin like following screenshots:


    Dynamics CRM MVP
    My blog

    • Marked as answer by JamesGRM Thursday, October 15, 2015 1:27 PM
    Thursday, October 15, 2015 1:07 PM
    Moderator
  • Thanks Andrii,

    I didn't know that images needed to be specified in that way. I wrongly assumed they were global and available to call as and when you like.

    Adding the image as per your screenshot has stopped the error I was receiving.

    Many Thanks,

    James

    Thursday, October 15, 2015 1:28 PM