I'm implementing this capability into something I'm doing for the first time. I just ran into the same issue. What worked for me was not including key fields in the update. It defeats the purpose of the Upsert because I have to distinguish between a create, where I include the fields used in the key, and the update where I exclude them. I haven't done exhaustive testing to validate my presumptive findings but it may help if someone finds themselves stuck.
Update: I probably should read docs more closely. Never include the fields used as part of a key in the upsert. Just specify them or it in the key collection or as a single key in the Entity constructor. That will place the key values in the correct fields on create and won't try to update them on the update.
------------------------------
Ben Sexton
Cornerstone Advisors, Inc.
------------------------------
Original Message:
Sent: Sep 18, 2019 11:38 AM
From: Rodrick Hales
Subject: UpsertRequest Failing WIth Alternate Key
Thanks for your response. I have verified the KeyAttributeCollection has the attributes populated from the Alternate Key. Also, this entity has two (2) Alternate Keys. Does this present a problem for UpsertRequest?
// Create an ExecuteMultipleRequest object.
var requestWithResults = new ExecuteMultipleRequest() {
// Assign settings that define execution behavior: continue on error,
// return responses.
Settings = new ExecuteMultipleSettings() {
ContinueOnError = false,
ReturnResponses = true
},
// Create an empty organization request collection.
Requests = new OrganizationRequestCollection()
};
// Add a UpsertRequest for each entity to the request collection.
foreach( var entity in eCollection.Entities ) {
KeyAttributeCollection altKey = new KeyAttributeCollection();
altKey.Add( "l3t_line_number", entity[ "l3t_line_number" ] );
altKey.Add( "l3t_squadron_id",
entity.GetAttributeValue<EntityReference>( "l3t_org_squadron" )
.Id.ToString() );
altKey.Add( "l3t_takeoff_time_est_text",
entity.GetAttributeValue<DateTime>( "l3t_takeoff_time" )
.ToString() );
Entity newEnt = new Entity( "l3t_flights", altKey );
// Required fields
newEnt[ "l3t_program_site" ] =
entity.GetAttributeValue<EntityReference>( "l3t_program_site" );
newEnt[ "l3t_flightdate" ] =
entity.GetAttributeValue<DateTime>( "l3t_flightdate" );
newEnt[ "l3t_entry_type" ] =
entity.GetAttributeValue<OptionSetValue>( "l3t_entry_type" );
newEnt[ "l3t_addreason" ] = new OptionSetValue(
( int )AddReasonCode.AddReason.SCHEDULED );
newEnt[ "l3t_org_squadron" ] =
entity.GetAttributeValue<EntityReference>( "l3t_org_squadron" );
newEnt[ "l3t_document_number" ] =
entity.GetAttributeValue<string>( "l3t_document_number" );
UpsertRequest request = new UpsertRequest { Target = newEnt };
requestWithResults.Requests.Add( request );
} // foreach
Log.Debug( "Waiting for response..." );
// Display the results returned in the responses.
// Limit is 1000 or Throttle on BatchSize greater than 1000...
var responseWithResults =
( ExecuteMultipleResponse )_service.Execute( requestWithResults );
if( responseWithResults.IsFaulted ) {
var faultMsg = string.Empty;
for( int i = 0; i <= responseWithResults.Responses.Count; i++ ) {
faultMsg += i + ". " +
responseWithResults.Responses[ i ].Fault.Message + "\n";
} // foreach
Log.Error( faultMsg );
throw new Exception( faultMsg );
} // if} // if
------------------------------
Rodrick Hales
Vertex Aerospace
Madison
Original Message:
Sent: Sep 18, 2019 09:13 AM
From: Daryl LaBar
Subject: UpsertRequest Failing WIth Alternate Key
Have you checked that you're using the Attribute and not the key for your alternate key? (https://community.dynamics.com/crm/b/mscrmcustomization/posts/upsert-request-using-alternative-key) Might help to show your code.
------------------------------
Daryl LaBar
President, MVP
Gap Integrity
Fishers IN
Original Message:
Sent: Sep 17, 2019 11:47 PM
From: Rodrick Hales
Subject: UpsertRequest Failing WIth Alternate Key
The UpsertRequest from the Dynamics CRM SDK is failing with Alternate Key. I keep getting the following error message when I need to Update:
A record that has the attribute values [MY_ALT_KEY_ATTRIBUTES] already exists. The entity key [MY_ALT_KEY_NAME] requires that this set of attributes contains unique values. Select unique values and try again.
The create part of the UpsertRequest works very well. This only happens when I hope for the update.
Why would I get the error above ONLY when the update part is triggered?
I have tried this with Duplicate Detection for my entity turn off and still the same results.
Using Dynamics 365 v9.0, GCC tenant. Any suggestions would be greatly appreciated.
------------------------------
Rodrick Hales
Vertex Aerospace
------------------------------