Quantcast
Viewing all articles
Browse latest Browse all 10280

Plugin Error... Help!

Hi everyone,

I seem to get an error "The given key was not present in the dictionary".. Seems to be sometimes happening on create and always on the update.

I have goggled this error and it seems that I need to create a pre image on Update? I'm quite new in terms of developing Plugins so any advice would be really helpful.

I have commented out the section of the code which includes Pre Images. However, I'm not sure if I am on the right track,

namespace WeightedBant
{
    using System;
    using System.Collections.ObjectModel;
    using System.Globalization;
    using System.Linq;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;

    /// <summary>
    /// Base class for all Plugins.
    /// </summary>    
    public class WeightedBant : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            try
            {
                trace.Trace("Entering the plugin");
                //Entity preImageEntity = null;
                //if (context.PreEntityImages.Contains("PreImage") && context.PreEntityImages["PreImage"] is Entity)
                //{
                //    preImageEntity = (Entity)context.PreEntityImages["PreImage"];
                //}
                Entity entity = (Entity)context.InputParameters["Target"];
#region Populating Bant Score
                //Declaring variables that will hold the OptionSet Values. It will be set to 0 if the user does not choose
                trace.Trace("Storing the OptionSet Values to variables");
                //int authorityValue;

                //if (((OptionSetValue)entity.Attributes["ap_authority"]).Value != null || preImageEntity.Attributes.Contains("ap_authority"))
                //{
                //    authorityValue = ((OptionSetValue)entity.Attributes["ap_authority"]).Value;
                //}

                //else
                //{
                //    authorityValue = 0;
                //}
                var authorityValue = entity.Attributes.Contains("ap_authority") ? ((OptionSetValue)entity.Attributes["ap_authority "]).Value : 0;
                var needValue = entity.Attributes.Contains("ap_need")?((OptionSetValue)entity.Attributes["ap_need "]).Value : 0;
                var budgetValue = entity.Attributes.Contains("ap_budget")?((OptionSetValue)entity.Attributes["ap_budget "]).Value : 0;
                var timeValue = entity.Attributes.Contains("ap_time")?((OptionSetValue)entity.Attributes["ap_time "]).Value : 0;

                //Calculates the total bantScore based on the Option Set Values and populates the ap_overallscore field.
                trace.Trace("Variables now contain Value. Calculate total");
                int bantScore = authorityValue + needValue + budgetValue + timeValue;

                trace.Trace("Populate ap_overallscore");
                entity["ap_overallscore"] = bantScore;

#endregion

#region Populating Lead Rating
                //Populating Lead Rating to Cold,Warm or Hot based on Bant Score.
                //3 = Cold. 2 = Warm. 1 = Hot

                trace.Trace("Populate Lead Rating");
                if (bantScore < 5)
                {
                    ((OptionSetValue)entity.Attributes["leadqualitycode"]).Value = 3;
                }
                else if (bantScore >= 5 && bantScore <= 12)
                {
                    ((OptionSetValue)entity.Attributes["leadqualitycode"]).Value = 2;
                }
                else
                {
                    ((OptionSetValue)entity.Attributes["leadqualitycode"]).Value = 1;
                }
            }
#endregion
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(" CRM has encountered an error when calculating the Weighted Bant. Message: " + ex.Message + ". InnerException: " + ex.InnerException);
            }
        }
    }
}


Viewing all articles
Browse latest Browse all 10280

Trending Articles