
	declare @configurationGroupIdAffiliatedGuid uniqueidentifier = 'b696e645-a597-4990-a996-99289ad86387'

	--account create:
	declare @ConfigurationEntityTreeIdAffiliatedAccountCreateGuid uniqueidentifier = 'E31B8862-88A7-4C1A-B66F-0F680192F985'

	
	declare @configurationGroupIdAffiliated int
	select @configurationGroupIdAffiliated = id from ConfigurationGroups where guidkey=@configurationGroupIdAffiliatedGuid

	--Configuration Tables - add Affiliated parameters:
	--01
	--AffiliatedToken ENTITY:
	DECLARE @ConfigurationEntityTypes TABLE ([Id] [int] NOT NULL,[Title] [nvarchar](max) NOT NULL);

	---------------------------------------------------------------
	---------------------------------------------------------------
	--02 new Parameters for AffiliatedToken
	DECLARE @ConfigurationParameterTypes TABLE (Id int NOT NULL,Title nvarchar(max) NOT NULL);
	
	
	---------------------------------------------------------------
	--04 - create groups for 
	--	Affiliated account Entity
	--	Affiliated token entity
	DECLARE @ConfigurationGroups TABLE (EntityId int NOT NULL,GuidKey uniqueidentifier not null );
	
	--activate account:
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (14, @ConfigurationEntityTreeIdAffiliatedAccountCreateGuid) 


	MERGE INTO dbo.ConfigurationGroups AS TARGET  
		USING @ConfigurationGroups AS SOURCE  
		ON upper(TARGET.GuidKey) = upper(SOURCE.GuidKey)
		WHEN MATCHED THEN    
				UPDATE SET entityid = SOURCE.entityid
		WHEN NOT MATCHED BY TARGET THEN   
				INSERT (EntityId,GuidKey) VALUES (Source.EntityId,upper(source.GuidKey)) 
	OUTPUT $action, SOURCE.*;

	---------------------------------------------------------------
	---------------------------------------------------------------

	--activate account:
	declare @configurationGroupIdAffiliatedAccountCreate int

	--activate account:
	select @configurationGroupIdAffiliatedAccountCreate = id from ConfigurationGroups where guidkey=@ConfigurationEntityTreeIdAffiliatedAccountCreateGuid
	---------------------------------------------------------------
	---------------------------------------------------------------
	--05 - create groups for Affiliated Entities
		
	DECLARE @ConfigurationGroupParameters TABLE (GroupId int NOT NULL,ParameterId int not null, Value nvarchar(max));
	

	--Affiliated activate account
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountCreate,75,  N'https://sb.alertmessage.com/acct/{0}'		-- N'Url')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountCreate,76,  N'affiliated-api'					-- N'ClientId')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountCreate,77,  N'application/x-www-form-urlencoded'	-- N'ContentType')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountCreate,78,  N'PUT'								-- N'Method')


	MERGE INTO dbo.ConfigurationGroupParameters AS TARGET  
			USING @ConfigurationGroupParameters AS SOURCE  
			ON TARGET.GroupId  = SOURCE.GroupId
			AND TARGET.ParameterId =  SOURCE.ParameterId
			WHEN MATCHED THEN    
					UPDATE SET Value=source.Value
			WHEN NOT MATCHED BY TARGET THEN   
					INSERT ( GroupId,ParameterId,Value) VALUES (Source.GroupId, SOURCE.ParameterId,source.Value)  
	OUTPUT $action, SOURCE.*;


	---------------------------------------------------------------
	---------------------------------------------------------------
	--06: ConfigurationEntityTree
	--Set the identifier in the tree for the instance
	
	declare @ConfigurationEntityTreeIdAffiliatedAlarmGuid uniqueidentifier = 'A4E36C1F-6B1F-4029-BE7C-7AE97F95DDF1'


	DECLARE @ConfigurationEntityTree TABLE (EntityTypeId int not null,GroupId int NOT NULL, MachineName nvarchar(max), ServiceName nvarchar(max), ServiceId nvarchar(max), 
											ModuleIdentifier nvarchar(max),ParentEntityId int null,GuidKey uniqueidentifier not null );


	declare @ConfigurationEntityTreeIdAffiliatedParentId int
	select @ConfigurationEntityTreeIdAffiliatedParentId = id from ConfigurationEntityTree where guidkey=@ConfigurationEntityTreeIdAffiliatedAlarmGuid

	--Affiliated Account Create
	INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
	select 14, @configurationGroupIdAffiliatedAccountCreate, N'MachineId', N'Affiliated', N'AffiliatedDefault',N'AffiliatedAccountCreate',@ConfigurationEntityTreeIdAffiliatedParentId, @ConfigurationEntityTreeIdAffiliatedAccountCreateGuid


	MERGE INTO dbo.ConfigurationEntityTree AS TARGET  
			USING @ConfigurationEntityTree AS SOURCE  
			ON upper(TARGET.GuidKey) = upper(SOURCE.GuidKey)
             
			WHEN MATCHED THEN    
					UPDATE SET  MachineName=source.MachineName, ServiceName=source.ServiceName, ServiceId=source.ServiceId, 
					ModuleIdentifier=source.ModuleIdentifier, ParentEntityId=source.ParentEntityId
			WHEN NOT MATCHED BY TARGET THEN   
					INSERT ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId,GuidKey) 
					VALUES ( source.EntityTypeId, source.GroupId, source.MachineName, source.ServiceName, source.ServiceId, source.ModuleIdentifier, source.ParentEntityId, upper(source.GuidKey))
	OUTPUT $action, SOURCE.*;