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
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");
}
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.
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,
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:)
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!
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,
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.
It should not be doing it with entity["attributeName"] syntax, but, maybe, there is another plugin involved?
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156