In this blog you can find: Useful and simple code snippets in the area of Dynamics CRM (T-SQL, PowerShell). Interesting topics about (TIBCO) Scribe Insight.

Friday, July 10, 2020

Retrieve Subsidiary Businessunits (hierarchy) via T-SQL from CRM Database

USE CRM_MSCRM
GO
DECLARE @rootName NVARCHAR(255) = 'CRM' /* Parent Business Unit Name */
DECLARE @SubsidiaryBusinesseUnits TABLE
 (businessunitid UNIQUEIDENTIFIER PRIMARY KEY, depth INT)
DECLARE @depth INT = 0
INSERT @SubsidiaryBusinesseUnits
 SELECT businessunitid ,@depth FROM BusinessUnitBase WHERE Name = @rootName
WHILE (@@rowcount > 0)
BEGIN
 SELECT @depth = @depth + 1
 INSERT @SubsidiaryBusinesseUnits
 SELECT t.BusinessUnitId
  ,@depth
 FROM BusinessUnitBase t
 WHERE EXISTS (
   SELECT *
   FROM @SubsidiaryBusinesseUnits s
   WHERE s.depth = (@depth - 1)
    AND s.businessunitid = t.ParentBusinessUnitId
   )
END
SELECT * FROM @SubsidiaryBusinesseUnits


SAMPLE: CRM Business Unit Structure


SQL Result


No comments:

Post a Comment

MSMQ - Add user privileges to queues via PowerShell

In PowerShell, the Set-MsmqQueueACL cmdlet can be used to modify the access rights of queues. Sample - Add "FullControl" permissi...