Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

CRM Plugins Error - An item with the same key has already been added

Posted on by Microsoft Employee

Hi,

In our Opportunity Update Post-Operation Synchronous plugin, we got the following error:

An item with the same key has already been added


Here's the stack trace:

at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Update(Entity entity, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode) at Microsoft.Crm.Extensibility.InprocessServiceProxy.UpdateCore(Entity entity) at CRMPlugIns.Opportunity.Opportunity_Update_Post.ExecuteLogic()


I don't understand how this is possible since I don't use the entity.Attributes.Add(key, value) method. I simply set the value of an attribute like this: entity["attributeName"] = attribute value.

Any ideas?

Thank you!

*This post is locked for comments

  • Suggested answer
    Varun Singh Profile Picture
    Varun Singh 941 on at
    RE: CRM Plugins Error - An item with the same key has already been added

    You can also do before assigning the key you can check below

    if ( !PluginExecutionContext.SharedVariables.Contains("keyname")){

    // then assign key

    PluginExecutionContext.SharedVariables.Add("keyname", "XXXXXX");

    }

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM Plugins Error - An item with the same key has already been added

    Hi Jeet - Thanks for the reminder!

    I asked this same question on the CRMUG forums and got a response worth sharing. Please see below:

    entity.Attributes.Add(key, value) is equivalent to entity[key] = value;
    
    Use the SetAttributeValue
    
    msdn.microsoft.com/.../microsoft.xrm.sdk.entity.setattributevalue.aspx

    Here's a combined list of possible solutions:

    I will mark this answer as the suggested answer so that it's easily visible to individuals with a similar error. Let me know if any of you have a problem with this decision.

    Thank you.

  • Jeet Gandhi Profile Picture
    Jeet Gandhi 320 on at
    RE: CRM Plugins Error - An item with the same key has already been added

    Hi Anatoliy,

    Thanks for your reply, I suggest you like the answers if you found them helpful. Also please let me know if you require further help for any queries.

    Thanking you,

  • Suggested answer
    Preeti Sharma Profile Picture
    Preeti Sharma 2,678 on at
    RE: CRM Plugins Error - An item with the same key has already been added

    Hi,

    Please check duplicate detection rules enabled for the entity as well.tlThere can be a rule being applied for the field.

    Hope this helps:)

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM Plugins Error - An item with the same key has already been added

    Alex: it's possible. Altough, I haven't seen this error occur for the past 2 weeks. I'm starting to think the code changes we made recently to refactor the plugin fixed the issue.

    James: I can't debug the plugin unless I create a console project to emulate an entity being updated. Glad you asked this question. We really should start thinking about setting up mock tests for all our Plugin methods.

    Jeet: Good idea. I think we will enable traces when we see this error happening consistently. It's been 2 weeks since I last saw this error.

    Thank you all for your response. Some very good ideas came out of this!

  • Jeet Gandhi Profile Picture
    Jeet Gandhi 320 on at
    RE: CRM Plugins Error - An item with the same key has already been added

    Hi,

    Anatoliy

    Please provide the complete code you have used as it seems like the error is coming from any other part of the plugin.

    Also, I suggest you to enable the traces in the plugin in order to get the exact location of the cause of the error.

    Thank you,

  • Suggested answer
    jestuder Profile Picture
    jestuder 156 on at
    RE: CRM Plugins Error - An item with the same key has already been added

    Can you debug the plugin and the specific method?  If so you best beat is to input a break point right before the error happens in your code.  You might also want to add a try catch around your block of code to get even more details.

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: CRM Plugins Error - An item with the same key has already been added

    It should not be doing it with entity["attributeName"] syntax, but, maybe, there is another plugin involved?

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,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans