Customer Engagement & Dynamics CRM Forum

Expand all | Collapse all

UpsertRequest Failing WIth Alternate Key

  • 1.  UpsertRequest Failing WIth Alternate Key

    Posted Sep 17, 2019 11:48 PM
    Edited by Rodrick Hales Sep 17, 2019 11:55 PM
    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
    ------------------------------
    Conference-CRMUG_200x200


  • 2.  RE: UpsertRequest Failing WIth Alternate Key

    MICROSOFT MVP
    Posted Sep 18, 2019 09:14 AM
    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
    ------------------------------

    Conference-CRMUG_200x200


  • 3.  RE: UpsertRequest Failing WIth Alternate Key

    Posted Sep 18, 2019 11:38 AM
    Edited by Rodrick Hales Sep 18, 2019 12:36 PM
    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
    ------------------------------

    Conference-CRMUG_200x200


If you've found this thread useful, dive deeper into User Group community content by role