Search code examples
c#pluginsdynamics-crmdynamics-365

The given key was not present in the dictionary (plugin for dynamics 365)


I've made a plugin for primary entity 'incident' which is triggered on pre-operation stage. I am getting the error 'The given key was not present in the dictionary'. I am not sure what could be wrong here since it's my first plugin. The aim is to check if Case's serial number (zst_txtsn) exists in entity 'Warranty'

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;
                }
            }
        }
    }
}

Any help would be much appreciated.

Solution

  • When the object is null, .ToString() will throw an error. You can try one of these.

    string serialNumber = Case.Attributes["zst_txtsn"] + "";
    

    or

    string serialNumber = "";
    if(Case.GetAttributeValue<string>("zst_txtsn") != null)
    {
        serialNumber = Case.GetAttributeValue<string>("zst_txtsn").ToString();
    }