Morgana
Morgana

Reputation: 337

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.

Upvotes: 0

Views: 4452

Answers (2)

ef_
ef_

Reputation: 28

Few things to note

1- If this plugin is registered on the create event The service.update(case) will fail because Target has no id yet In this case any updates should be done on the Target without a service.update

2- If this plugin is registered on the update event it’ll cause a loop if you’re not setting the filtering attributes properly for the trigger in the plugin registration tool. Additionally, since it’s on the pre stage, it should be enough to update the target with your boolean value to pass it to the platform.

In both cases above you should remove the service.update

3- If zst_serialno is a lookup and you’re querying using the serial number text value Then your query is wrong You’ll have to use LinkEntities / $expand to query by related entity attributes

Upvotes: 1

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

Upvotes: 1

Related Questions