CRM 2013 System.IndexOutOfRangeException: privilegeid error solution

System.IndexOutOfRangeException: privilegeid Error

Hello everyone,

In this post I will share how to solve the System.IndexOutOfRangeException: privilegeid error that you can get when assigning roles to users.

If you recently worked with BusinessUnits and Teams you may have the same error.

Before applying the fix I will share below make sure that you error message detail includes RetrievePrivilegeMaxDepthFromTeamRoles as well.

When I check the Trace logs I could see below Error detail.

 

If you also got this error message detail you can apply the fix below. Just simply copy and paste the sql script code below into a new query window in your Sql Management Studio and change [YOURORGANIZATION_MSCRM] part with your CRM Organization Database name.

 

 Sql Script:

USE [YOURORGANIZATION_MSCRM]
GO
/****** Object:  StoredProcedure [dbo].[p_RetrievePrivilegeMaxDepthFromTeamRoles] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[p_RetrievePrivilegeMaxDepthFromTeamRoles](@userId uniqueidentifier) AS
BEGIN

	SET NOCOUNT ON

	-- Collect only unique ParentRootRoleId and do aggregation for those roles only
	-- (Inherited roles are readonly and reuse ParentRootRoleId RolePrivileges)
	;WITH UniqueRootRoles(ParentRootRoleId)
	as
	(
 	SELECT DISTINCT r.ParentRootRoleId
	  FROM Role r with (nolock)
	       INNER JOIN TeamRoles tr with (nolock) on tr.RoleId = r.RoleId
	       INNER JOIN TeamMembership tm with (nolock) on tm.SystemUserId = @userId and tm.TeamId = tr.TeamId
	)

	-- Retrieve privilege information for all roles assigned to teams in which user has membership.
SELECT p.PrivilegeId 'privilegeid', p.IsDisabledWhenIntegrated 'isdisabledwhenintegrated', p.AccessRight 'accessright', potc.ObjectTypeCode 'objecttypecode', max(rp.PrivilegeDepthMask) as 'privilegedepthmask'
FROM RolePrivileges rp with (nolock)
	       INNER JOIN UniqueRootRoles r on r.ParentRootRoleId = rp.RoleId
	       INNER JOIN Privilege p on p.PrivilegeId = rp.PrivilegeId
	       INNER JOIN PrivilegeObjectTypeCodes potc on potc.PrivilegeId = p.PrivilegeId
	GROUP BY p.PrivilegeId, p.IsDisabledWhenIntegrated, p.AccessRight, potc.ObjectTypeCode
	ORDER BY p.PrivilegeId

END

Hope this helps!

Leave a Reply

Your email address will not be published. Required fields are marked *