I have a custom action which clones the product entity using the code snippet below
List<string> ignoreAttributes = new List<string>() { "productid", "productnumber", "statecode"};
Entity clonedProduct = Clone(sourceProduct, new Entity("product"), ignoreAttributes);
private Entity Clone(Entity source, Entity cloned, List<string> ignoreAttributes)
{
foreach (var attribute in source.Attributes)
{
string attributeName = attribute.Key;
object attributeValue = attribute.Value;
if (ignoreAttributes.Contains(attributeName)) continue;
cloned[attributeName] = attributeValue;
}
return cloned;
}
Now this code works fine for almost every product except few in the production. I have tried to log the key attributes being cloned but there is not any. Can someone please help me in identifying what is this key which is throwing this error for some particular products only? Any idea how I can check what is this key name which is causing issues?
Below is the exception being logged
Unhandled exception: Exception type: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault] Message: Cannot insert duplicate key.Detail: <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts"> <ActivityId>e111d6d8-d8b3-4db6-87e0-df7d1c1eddba</ActivityId> <ErrorCode>-2147220891</ErrorCode> <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic"> <KeyValuePairOfstringanyType> <d2p1:key>OperationStatus</d2p1:key> <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema" i:type="d4p1:int">0</d2p1:value> </KeyValuePairOfstringanyType> <KeyValuePairOfstringanyType> <d2p1:key>SubErrorCode</d2p1:key> <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema" i:type="d4p1:int">-2146233088</d2p1:value> </KeyValuePairOfstringanyType> </ErrorDetails> <Message>Cannot insert duplicate key.</Message> <Timestamp>2019-01-04T06:50:54.3872309Z</Timestamp> <ExceptionRetriable>false</ExceptionRetriable> <ExceptionSource>PluginExecution</ExceptionSource> <InnerFault i:nil="true" /> <OriginalException>Microsoft.Xrm.Sdk.InvalidPluginExecutionException at System.Activities.WorkflowApplication.Invoke(Activity activity, IDictionary`2 inputs, WorkflowInstanceExtensionManager extensions, TimeSpan timeout) at System.Activities.WorkflowInvoker.Invoke(Activity workflow, IDictionary`2 inputs, TimeSpan timeout, WorkflowInstanceExtensionManager extensions) at System.Threading.Tasks.Task`1.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.Crm.Sandbox.SandboxAppDomainHelper.Execute(IOrganizationServiceFactory organizationServiceFactory, String customActivityTypeName, IExecutionContext requestContext, Dictionary`2 sandboxServices, Boolean useDrawbridgeEnabled, Boolean chaosFailAppDomain) at Microsoft.Crm.Sandbox.SandboxAppDomainHelper.Execute(IOrganizationServiceFactory organizationServiceFactory, String customActivityTypeName, IExecutionContext requestContext, Dictionary`2 sandboxServices, Boolean useDrawbridgeEnabled, Boolean chaosFailAppDomain) at Microsoft.Crm.Sandbox.SandboxWorker.ExecuteCustomWorkflowActivity(SandboxCallInfo callInfo, SandboxCustomActivityExecutionContext requestContext, Guid pluginAssemblyId, Int32 sourceHash, String assemblyName, Guid pluginTypeId, String pluginTypeName, SandboxRequestCounter& workerCounter, Boolean returnTraceInfo)</OriginalException>






