Quantcast
Channel: Forum CRM Development
Viewing all articles
Browse latest Browse all 1000

CRM 2011 plugin: system.ArgumentException: An item with the same key has already been added

$
0
0

Hi All,

I am new to plugins development. I am trying to retrieve 3 attributes from an entity called 'sample entity', then subtract 2 attributes and update the third one.

example: attributes are

field 1, field 2, field 3. 

field 3 = field 1 - field 2. These attributes are decimal numbers in CRM 2011

I want the plugin to fire when the form is updated. So I have made the execution on post-operation.

The code is below,

                                                  

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


namespace samplePlugin
{
    public class Plugin : IPlugin
    {
        public void Execute(IServiceProvider serviceprovider)
        {
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceprovider.GetService(typeof(IPluginExecutionContext));


            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parmameters.
                Entity entity = (Entity)context.InputParameters["Target"];

           

           try
                {
                  // Retrieve the days used and subtract it from Total Allowance
                         
                  decimal oDaysUsed = entity.GetAttributeValue<decimal>("tcrmb_daysused");
                  decimal oTotalAllowance = entity.GetAttributeValue<decimal>("tcrmb_totalallowance");
                 // decimal oDaysAvailable = entity.GetAttributeValue<decimal>("tcrmb_daysavailable");

                  decimal oAvailable = oTotalAllowance - oDaysUsed;
                  entity.Attributes.Add("tcrmb_daysavailable", oAvailable);               
                }



                catch (FaultException ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                }
            }
        }
    }
}

Kindly have a look and let me know please

Regards,

Paul



Viewing all articles
Browse latest Browse all 1000

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>