Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

Given key was not present in the dictionary

Posted on by 165

Hello,

I am not sure why I am getting this error, below is my code for the plugin

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;

namespace ActiveWarranty
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{

ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));


IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));


if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{

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


IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

try
{
string serialNumber = Case.Attributes["zst_txtsn"].ToString();
Entity Warranty = new Entity("zst_warranty");

QueryExpression query = new QueryExpression("zst_warranty");
query.ColumnSet = new ColumnSet(new string[] { "zst_name" });
query.Criteria.AddCondition("zst_serialno", ConditionOperator.Equal, serialNumber);

EntityCollection collection = service.RetrieveMultiple(query);

if (collection.Entities.Count > 0)
{
Case["zst_activewarranty"] = true;
}
else if (collection.Entities.Count == 0)
{
Case["zst_activewarranty"] = false;
}

service.Update(Case);
}

catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
}

catch (Exception ex)
{
tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
throw;
}
}
}
}
}
  • Suggested answer
    Syed Ibrahim Profile Picture
    Syed Ibrahim 6,257 on at
    RE: Given key was not present in the dictionary

    Use this updated code..

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using Microsoft.Xrm.Sdk;

    using System.ServiceModel;

    using Microsoft.Xrm.Sdk.Query;

    namespace ActiveWarranty

    {

       public class Class1 : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               ITracingService tracingService =

                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

               IPluginExecutionContext context = (IPluginExecutionContext)

               serviceProvider.GetService(typeof(IPluginExecutionContext));

               if (context.InputParameters.Contains("Target") &&

               context.InputParameters["Target"] is Entity)

               {

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

                   IOrganizationServiceFactory serviceFactory =

                   (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                   IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                   try

                   {

                       if (Case.Attributes.Contains("zst_txtsn"))

                       {

                           string serialNumber = Case.Attributes["zst_txtsn"].ToString();

                           Entity Warranty = new Entity("zst_warranty");

                           QueryExpression query = new QueryExpression("zst_warranty");

                           query.ColumnSet = new ColumnSet(new string[] { "zst_name" });

                           query.Criteria.AddCondition("zst_serialno", ConditionOperator.Equal, serialNumber);

                           EntityCollection collection = service.RetrieveMultiple(query);

                           if (collection.Entities.Count > 0)

                           {

                               Case["zst_activewarranty"] = true;

                           }

                           else if (collection.Entities.Count == 0)

                           {

                               Case["zst_activewarranty"] = false;

                           }

                           service.Update(Case);

                       }

                   }

                   catch (FaultException<OrganizationServiceFault> ex)

                   {

                       throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);

                   }

                   catch (Exception ex)

                   {

                       tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());

                       throw;

                   }

               }

           }

       }

    }

    Hope this will work,

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Given key was not present in the dictionary

    Hi,

    I have updated your code on other forum question posted by you. Please check that and let me know.

    Please mark my answer verified if i were helpful

  • irenegr Profile Picture
    irenegr 165 on at
    RE: Given key was not present in the dictionary

    Hello,

    the problem seems to be connected to GUID:

    FaultException`1: Condition for attribute 'zst_warranty.zst_serialno': expected argument(s) of type 'System.Guid' but received 'System.DBNull'.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Given key was not present in the dictionary

    Hello ,

    You should follow above suggested answers, apart from that I would suggest try to debug your plugin and write trace log you can find root cause easily  for this kind of issue.

    docs.microsoft.com/.../logging-tracing

    docs.microsoft.com/.../tutorial-debug-plug-in

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Given key was not present in the dictionary

    Hi,

    You should always check if attribute contains in entity object. see below.

    Replace this line of code

    string serialNumber = Case.Attributes["zst_txtsn"].ToString();

    with below

    string serialNumber = String.Empty;

    if(Case.Attributes.Contains("zst_txtsn"))

    {

    serialNumber =Case.Attributes["zst_txtsn"].ToString();

    }

    Then check if serialNumber  is not null or empty.

    Please mark my answer verified if i were helpful

  • Suggested answer
    RE: Given key was not present in the dictionary

    Hi,

    Can you double check if those fields name are correct?

    
    
    This type of error usually happens when we try to access the collection and we're passing a non-existing key. For example, 
    Case["zst_activewarranty"] = true;


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans