locked
AddUserToRecordTeamRequest returns error: An object with the specified name already exists RRS feed

  • Question

  • MS-CRM 2013, SP1, UR1, on premisses

    We have a plug-in which maintains access using access teams. In general it works as intended, except when (another plugin/program/whatever) fires a number of requests against the same record instance.

    Scenario:

    an account exists with no access team created
    give 5 users access to the account using AddUserToRecordTeamRequest
    since all/most of the requests are executed in parallel, request no 2-5 do not recognize that the access team has been created
    error An object with the specified name already exists is returned

    public static void AddUserToTeam(IOrganizationService service, Guid UserId, Guid teamTemplateId, EntityReference entityToAdd)
    {
        // Add user to system generated access team
               
        try
        {
            if (!UserIsActive(service, UserId)) { return; }
            if (UserIsTeamMember(service, UserId, teamTemplateId, entityToAdd.Id)) { return; }
    
            AddUserToRecordTeamRequest adduser = new AddUserToRecordTeamRequest()
            {
                Record = entityToAdd,
                SystemUserId = UserId,
                TeamTemplateId = teamTemplateId
            };
            AddUserToRecordTeamResponse response = (AddUserToRecordTeamResponse)service.Execute(adduser);
        }
        catch (Exception ex)
        {
            throw new InvalidPluginExecutionException("An error occurred ...", ex);
        }
    }

    I started a full trace an can see that the system tries to create multiple team records with the same name:

    >System.Data.SqlClient.SqlException (0x80131904): Cannot insert duplicate key row in object 'dbo.TeamBase' with unique index 'ndx_TeamName_BusinessUnitId'. The duplicate key value is (6b858c7e-297c-e211-9ac5-00219b2eb12d+ffc40c85-917c-e311-bca4-005056936680, e71926ab-8955-e211-b743-00505693003b).


    Per



    Friday, October 31, 2014 5:48 PM

Answers

  • I think you cannot completely avoid this due to the way the AddUserToRecordTeam message works. However, I think you can code around it by catching the exception, and resubmitting the AddUserToRecordTeam request. By that time the CRM platform should correctly identify that the generated team record already exists

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    • Marked as answer by PerGeert too Saturday, November 1, 2014 7:07 PM
    Saturday, November 1, 2014 6:20 PM
    Moderator

All replies

  • it is possible that the current user may not have permission to get team membership. So your UserIsTeamMember returns false. So trying to add a user to already existing team may be throwing exception.

    regards

    Jithesh

    Saturday, November 1, 2014 5:27 AM
  • That is not the case, i.e. when you run the program again to give the 5 users access, the remaining 4 are added, because this time the access team record already exists, and the api call work as expected.

    Per

    Saturday, November 1, 2014 1:56 PM
  • I think you cannot completely avoid this due to the way the AddUserToRecordTeam message works. However, I think you can code around it by catching the exception, and resubmitting the AddUserToRecordTeam request. By that time the CRM platform should correctly identify that the generated team record already exists

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    • Marked as answer by PerGeert too Saturday, November 1, 2014 7:07 PM
    Saturday, November 1, 2014 6:20 PM
    Moderator
  • Thanks - I've try all sorts of ways to implement a semaphore, adding delays etc. However, your idea I hadn't had - and it worked. Added this piece of code:

                catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
                {
                    // Catch error code -2147220950 (team duplicate error)
                    if (ex.Detail.ErrorCode == -2147220950)
                    {
                        // Try again
                        Thread.Sleep(50);
                        AddUserToTeam(service, UserId, teamTemplateId, entityToAdd);
                        return;
                    }
    
                    throw new InvalidPluginExecutionException("An error occurred in the XXX.Sharing.AddUserToTeam plugin." +
                    " \nUserid=" + UserId.ToString() +
                    " \nTemplateid=" + teamTemplateId.ToString() +
                    " \nEntityid=" + entityToAdd.Id.ToString() +
                    " \nEntitytype=" + entityToAdd.LogicalName +
                    " \n", ex);
                }


    Per


    Saturday, November 1, 2014 7:07 PM