locked
Plugin error - The given key was not present in the dictionary RRS feed

  • Question

  • I have a plugin with FetchXML query that executes on update of the Opportunity entity; the code is below:

     // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parmameters.
                    Entity entity = (Entity)context.InputParameters["Target"];
    
                    //get the customerid
                    EntityReference a = (EntityReference)entity.Attributes["customerid"];
    
                    decimal totalAmount = 0;
    
                    try
                    {
                        //fetchxml to get the sum total of estimatedvalue
                        string estimatedvalue_sum = string.Format(@" 
                        <fetch mapping='logical' aggregate='true'>
                         <entity name='opportunity'>
                             <attribute name='estimatedvalue' aggregate='sum' alias='estimatedvalue_sum' />
                         </entity>
                        </fetch>");
                        EntityCollection estimatedvalue_sum_result = service.RetrieveMultiple(new FetchExpression(estimatedvalue_sum));
    
                        foreach (var c in estimatedvalue_sum_result.Entities)
                        {
                            totalAmount = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
                        }
    
                        //updating the field on the account
                        //Entity acc = new Entity("account");
                        //acc.Id = a.Id;
                        //acc.Attributes.Add("new_oppamount", new Money(totalAmount));
                        //service.Update(acc);

    The problem I am having is that the line: 

    EntityReference a = (EntityReference)entity.Attributes["customerid"];

    Gives an error: "The given key was not present in the dictionary." I'm not sure how to debug because I am using CRM Online. Anyone know what's up?



    Wednesday, November 20, 2013 3:14 PM

Answers

  • Hi,

    If your plugin is triggering on update event then you will need to register PostImage to get customerid field data.

     Entity entity = (Entity)context.InputParameters["Target"];

    Above entity object can have updated fields only.

    So, try like as below :

    if (context.PostEntityImages.Contains("PostImage"))
    {
    Entity PostImage = (Entity)context.PostEntityImages["PostImage"];
    EntityReference a = (EntityReference) PostImage.Attributes["customerid"];
    // rest of the code
    }
    


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !

    • Marked as answer by Syed..Hussain Wednesday, November 20, 2013 5:13 PM
    Wednesday, November 20, 2013 3:28 PM
  • Hi Hussain,

    Make sure whether the attribute customerid is available or not,

    if (entity.Attribute.Contains("customerid"))
    {
     EntityReference a = (EntityReference)entity.Attributes["customerid"];
    }

    If you are using context.InputParameters["Target"], then you will get only updated values.

    Instead of below code

         if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parmameters.
                    Entity entity = (Entity)context.InputParameters["Target"];

    u can replace with below line

    Entity entity = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true)); 

    Thanks,

    Sravan J.

    If it is useful to u please mark as a answer


    • Edited by sravan J Wednesday, November 20, 2013 3:34 PM modify
    • Marked as answer by Syed..Hussain Wednesday, November 20, 2013 5:13 PM
    Wednesday, November 20, 2013 3:32 PM

All replies

  • Hi,

    If your plugin is triggering on update event then you will need to register PostImage to get customerid field data.

     Entity entity = (Entity)context.InputParameters["Target"];

    Above entity object can have updated fields only.

    So, try like as below :

    if (context.PostEntityImages.Contains("PostImage"))
    {
    Entity PostImage = (Entity)context.PostEntityImages["PostImage"];
    EntityReference a = (EntityReference) PostImage.Attributes["customerid"];
    // rest of the code
    }
    


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !

    • Marked as answer by Syed..Hussain Wednesday, November 20, 2013 5:13 PM
    Wednesday, November 20, 2013 3:28 PM
  • Hi Hussain,

    Make sure whether the attribute customerid is available or not,

    if (entity.Attribute.Contains("customerid"))
    {
     EntityReference a = (EntityReference)entity.Attributes["customerid"];
    }

    If you are using context.InputParameters["Target"], then you will get only updated values.

    Instead of below code

         if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parmameters.
                    Entity entity = (Entity)context.InputParameters["Target"];

    u can replace with below line

    Entity entity = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true)); 

    Thanks,

    Sravan J.

    If it is useful to u please mark as a answer


    • Edited by sravan J Wednesday, November 20, 2013 3:34 PM modify
    • Marked as answer by Syed..Hussain Wednesday, November 20, 2013 5:13 PM
    Wednesday, November 20, 2013 3:32 PM
  • Thank you for the replies, it seems to work on update and not on create, I am trying to get the plugin to trigger on all Create, Update and Delete.
    Wednesday, November 20, 2013 4:11 PM
  • Hi,

    You could track the event/message inside plugin and write your login based on the message

    if (context.MessageName == "Create") { // write your earliest code here } else if (context.MessageName == "Update") { // write your current code here } else if (context.MessageName == "Delete") {

    // write delete message code here }

    For delete message you have to register pre image to get customerid field data.


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !


    • Edited by _Vikram Wednesday, November 20, 2013 4:36 PM
    Wednesday, November 20, 2013 4:33 PM
  • VIkram, thank you for the code. I've actually added that to another plugin. However, I'm sorry I didn't explain well in my first post.

    In the Opportunity Entity, I have a lookup to a custom entity called new_servicecontract; based on what you provided I changed the entity reference to point to this:

    EntityReference a = (EntityReference)entity.Attributes["new_servicecontract"];

    However this is where the error occurs: "An error occurred in the plug-in.". So, here I am unable to execute the code on create,update,delete.

    Wednesday, November 20, 2013 5:08 PM
  • Vikram, Sravan - thank you I sorted it out.
    Wednesday, November 20, 2013 5:12 PM