locked
Given key not present in the dictionary RRS feed

  • Question

  • Aaaarrggghhhhh.

    I tried writing my own plugin after experimenting with the plugin walkthroughs in the CRM 4.0 SDK.  I wrote, signed, compiled, deployed, registered the assembly.  The assembly creates a new Task on update of a Case with all the Case notes in the new Task description.  The registered step is setup as as Post Stage and synchronous, on Update of incident entity.  I'm getting the error below.  I have noooo idea what I'm doing wrong.  I'm very new to writing plugins for CRM.  I can't tell if this is something in my code or my deployment.

    Thanks in advance for any help.

    ------------------------------------------

    Web Service Plug-in failed in OrganizationId: d68b4a7e-bab3-46d8-a192-2485a3e5fc9f; SdkMessageProcessingStepId: 23108d49-a1a4-de11-9a58-000cf1ad3811; EntityName: incident; Stage: 50; MessageName: Update; AssemblyName: Reactornet.Crm.Plugin.CopyNotesToCase, Reactornet.Crm.Plugin, Version=1.0.0.1, Culture=neutral, PublicKeyToken=6447c3e4e388b180; ClassName: Reactornet.Crm.Plugin.CopyNotesToCase; Exception: Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

    at System.ThrowHelper.ThrowKeyNotFoundException()at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
    at Microsoft.Crm.Sdk.PropertyBag.get_Item(String key)
    at Reactornet.Crm.Plugin.CopyNotesToCase.Execute(IPluginExecutionContext context)
    at Microsoft.Crm.Extensibility.PluginStep.Execute(PipelineExecutionContext context)

    --------------------------------------------

    Here is my code.  It was borrowed from some code posted on this newsgroup.  It's written in Delphi.NET (which is now Oxygene (Pascal)).

     

     

     

    method CopyNotesToCase.Execute(context : IPluginExecutionContext);
    var
       caseId : Guid;
       currentCase : DynamicEntity;
       service : ICrmService;
       req : RetrieveMultipleRequest;
       qe : QueryExpression;
       resp : RetrieveMultipleResponse;
       annotationStrings, taskDescription : StringBuilder;
       t : task;
    begin
    
       caseId := context..OutputParameters[ParameterName.Id] as Guid; 
       currentCase := context.InputParameters[ParameterName.Target] as DynamicEntity;
       service := context.CreateCrmService(true); 
       req := new RetrieveMultipleRequest(); 
    
       qe := new QueryExpression(EntityName.annotation.ToString()); 
       qe.Criteria.AddCondition('objectid', ConditionOperator.Equal, caseId); 
       qe.ColumnSet := new AllColumns(); 
       req.Query := qe; 
       resp := service.Execute(req) as RetrieveMultipleResponse; 
    
       annotationStrings := new StringBuilder(); 
    
       for each a1 : annotation in resp.BusinessEntityCollection.BusinessEntities do 
       begin 
         annotationStrings.AppendLine('<Start> '); 
         annotationStrings.Append(a1.subject); 
         annotationStrings.AppendLine(a1.notetext); 
         annotationStrings.AppendLine(' <End> '); 
       end;
    
       taskDescription := new StringBuilder(); 
       taskDescription.Append(currentCase['title'].ToString()); 
       taskDescription.Append(annotationStrings.ToString()); 
       t := new task(); 
       t.subject := 'SamplePlugin Task for ' + context.PrimaryEntityName + ' , stage = ' + context.Stage.ToString() +' , mode = ' + context.Mode.ToString() + ' , at ' + DateTime.Now.ToString(); 
       t.description := taskDescription.ToString();
    
       service.Create(t);
    end;
    
    • Edited by akylitis Friday, September 18, 2009 10:36 PM More Clarification
    Friday, September 18, 2009 10:34 PM

Answers

  • Ok.  With a little help from other postings, I figured this out.  Because the plugin is triggered on an update, the Id is not available in the OutputParameters property.  Had to get the Guid from the incidentid property of the target entity.

    Replaced...

    caseId := context..OutputParameters[ParameterName.Id] as Guid;

    With...

    entity := context.InputParameters.Properties[ParameterName.Target]

    as DynamicEntity;
    try
    begin
         if (entity.Name = EntityName.incident.ToString()) then
    begin
         myKey := entity.Properties['incidentid'] as Key;
         caseId := myKey.Value
    as Guid;
         .....

    • Marked as answer by akylitis Saturday, September 19, 2009 3:31 AM
    Saturday, September 19, 2009 3:31 AM

All replies

  • Well, this is obviously something in my code, most likely.  I must be refering to a non existent property.  This posting is similar to my problem:

    http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/25086899-92d4-4b7c-be03-343418de7998/

    Any help much appreciated.
    Friday, September 18, 2009 11:17 PM
  • Ok.  With a little help from other postings, I figured this out.  Because the plugin is triggered on an update, the Id is not available in the OutputParameters property.  Had to get the Guid from the incidentid property of the target entity.

    Replaced...

    caseId := context..OutputParameters[ParameterName.Id] as Guid;

    With...

    entity := context.InputParameters.Properties[ParameterName.Target]

    as DynamicEntity;
    try
    begin
         if (entity.Name = EntityName.incident.ToString()) then
    begin
         myKey := entity.Properties['incidentid'] as Key;
         caseId := myKey.Value
    as Guid;
         .....

    • Marked as answer by akylitis Saturday, September 19, 2009 3:31 AM
    Saturday, September 19, 2009 3:31 AM