locked
Error “The given key was not present in the dictionary” When Creating New Custom Entity on Update Event RRS feed

  • Question

  • Hi,

    On CRM 2013 on-premise, I'm trying to write a plugin that triggers when an update is made to a field on Quote. The plugin then creates a new custom entity "new_contract".

    My plugin is successfully triggered when the update to that field is made (I try throwing InvalidPluginExecutionException and it works).

    However during the 'Create' I keep getting an error message "The given key was not present in the dictionary" when trying to create the new custom entity. I know it fails during the 'Create' event because if I remove it from my code I don't have any error.

    I'm using a "PostImage" in this code. I confirm that it's registered using the same name in Plugin Registration.

    Here is the code. Any help is appreciated.

    -tri

    			var targetEntity = context.GetParameterCollection<Entity>(context.InputParameters, "Target");
    
                if (targetEntity == null)
                    throw new InvalidPluginExecutionException(OperationStatus.Failed, "Target Entity cannot be null");
    
                var postImage = context.PostEntityImages["PostImage"];
    
                if (postImage == null)
                    throw new InvalidPluginExecutionException(OperationStatus.Failed, "Post Image is required");
    
                var quote = context.GenerateCompositeEntity(targetEntity, postImage);
                      
                var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                var service = serviceFactory.CreateOrganizationService(context.UserId);
    
                var contractEntity = new Entity();
                contractEntity = new Entity("new_contract");
    
                if (quote.Attributes.Contains("new_portfolio"))
                {
                    var quotePortfolio = (EntityReference)quote.Attributes["new_portfolio];
                    contractEntity[Schema.new_contract.PortfolioName] = new EntityReference(quotePortfolio.LogicalName, quotePortfolio.Id);
                }
    
                if (quote.Attributes.Contains(Schema.Quote.QuoteName))
                {
                    var quoteName = (string)quote.Attributes["name"];
                    contractEntity[Schema.new_contract.contractName] = quoteName;
                }
    
    
                var contractId = service.Create(contractEntity);

    Friday, October 16, 2015 9:14 PM

Answers

  • Hi,

    Thank you all for the inputs. The plugin is actually registered as PostEvent. However I found out that in my PostImage in PluginRegistration, all the fields needed for the operation were not selected. Thanks for all the hints.

    -tri 

    • Marked as answer by triangular Tuesday, October 20, 2015 7:06 PM
    Tuesday, October 20, 2015 7:05 PM

All replies

  • This error comes when you try to access any attribute but it is not present in the property beg, I will suggest you to catch Fault Exception which can give you pointer which field creating this issue.

    refer:https://msdn.microsoft.com/en-us/library/gg334685.aspx


    Mahender


    Saturday, October 17, 2015 7:01 AM
    Moderator
  • If you debug the code you'd find the line that throws the error. From what you've posted, the most likely cause is if the plugin is registered on a PreEvent - if so, there will be no PostEntityImage, so this line would fail

     var postImage = context.PostEntityImages["PostImage"];
    


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Saturday, October 17, 2015 2:11 PM
    Moderator
  • Hi,

    Thank you all for the inputs. The plugin is actually registered as PostEvent. However I found out that in my PostImage in PluginRegistration, all the fields needed for the operation were not selected. Thanks for all the hints.

    -tri 

    • Marked as answer by triangular Tuesday, October 20, 2015 7:06 PM
    Tuesday, October 20, 2015 7:05 PM